Reputation: 109
This is my form to submit
@using (Html.BeginForm("SigningAsync", "Signing", FormMethod.Post,
new { onsubmit = "return confirm('Do you really want to submit the form?');"}))
{
<div class="col-md-6 col-sm-12 form-group" style="margin-bottom:5px !important">
<input type="submit"
id="btnConf"
name="Sign" value="Confirm"
class="btn btn-primary pull-right" style="margin-right:2px" />
<div class="form-group">
<input type="hidden" id="arr" name="arr" />
</div>
</div>
}
<input type="checkbox" value="@masterData.DocNo" name="selected" class="inforID" id="@masterData.NewDocNo" plant="@masterData.Plant" flowno="@masterData.FlowNo"/>
When checkbox is checked and Confirm button is pressed, I put attribute from checkbox to input arr
$(document).ready(function() {
$("#btnConf").click(function () {
var selected = [];
$.each($("input[name='selected']:checked"), function () {
selected.push({
DocNo: $(this).val(),
NewDocNo: $(this).attr("id"),
Plant: $(this).attr("plant"),
FlowNo: $(this).attr("flowno")
});
document.getElementById("arr").value = JSON.stringify(selected);
});
console.log(JSON.stringify(selected));
});
});
The input arr
will like this
<div class="form-group">
<input type="hidden" id="arr" name="arr" value="[
{"DocNo":"100400020719-006","NewDocNo":"ABS-02072019","Plant":"VDNF","FlowNo":"FLW-000001"},
{"DocNo":"100400020719-007","NewDocNo":"ABS-02072019","Plant":"VDNF","FlowNo":"FLW-000001"}
]">
</div>
And this is the Action
public async Task<ActionResult> SigningAsync([Bind(Include = "arr")] PurchaseRequestViewModel purchaseRequestViewModel, string Sign, string[] arr)
{
bool result = false;
if(arr != null)
{
if (ModelState.IsValid && !String.IsNullOrWhiteSpace(Sign))
{
for(int i = 0; i < arr.Length; i++)
{
string test = a[i].NewDocNo;
}
}
}
}
I can't get the property of object in arr
, I have tried string test = a[i][NewDocNo];
or string test = a[i]["NewDocNo"];
.
But it wasn't right. How can I get the properties of array in the Controller?
I have tried console.log(selected[0].NewDocNo);
in Javascript and it's ok, but in the Controller, it wasn't right.
Upvotes: 0
Views: 43
Reputation: 1705
The value that you have in your HTML will be treated as a string value as there are not multiple values passed to the controller. You are only passing a single value. You need to change the method signature and use string arr
and have the value as a string. Then do the conversion on the server.
Also, instead of submitting a form manually, better to use Javascript and AJAX to submit the data. Then you can pass the value of arr
in the request body as JSON. Then that will automatically be converted into an array.
Upvotes: 1