I have a cshtml as follow,
DoPost.cshtml
@using (Html.BeginForm("Purchase", "PurchaseOrder", FormMethod.Post, new { @id = "frmPurchase" }))
{
// statements
// statements
<input type="button" id="submitPurchase" onclick = "myPurchase()" value="Select" />
}
In Javascript
I have an array strings in variable "ExtraItems
"
ExtraItems[0] ="123"
ExtraItems[1] ="124"
ExtraItems[2] ="125"
My Action which accept the data is as follows,
public ActionResult Purchase(PurchaseOrderModel model)
{
//Do some stuff with the passed data
return View("Purchase", model);
}
In the above PurchaseOrderModel
, I have the property
public string[] SelectedProducts { get; set; }
to accept the Javascript Array elements.
What I tried:
The simple post did not work as the JavaScript array elements are not part of the Form elements,I couldn't use a @Html.HiddenFor
because it is an array.
Hence tried to do an Ajax post under function myPurchase()
,
$a.post('@Url.Action("Purchase", "PurchaseOrder")', { SelectedProducts: ExtraItems });
Here I did not get the ExtraItems
details under model.SelectedProducts
in the action. The biggest issue was i wanted to load the Purchase.cshtml
View from the action, instead I got the controll back to the Jquery Post.
Please help me how can I solve this.