I am trying to post data using jquery ajax to mvc action. But the problem is that whenever I am trying to serialize this form (which is a partial view), the Id which I have provided in html attributes of the form is not being recognised by jquery.This is the code :
@using(Ajax.BeginForm("AddMaterials" , "Bom" ,null, new AjaxOptions {
HttpMethod = "POST" } , new { @id = "ajaxBomMaterials" }))
And this is the code for serialize :
var data = $("#ajaxBomMaterials").serialize();
If I use "form" instead then it shows the serialzed data of the whole form(Main form as well as the partial view form) whereas I just need data for the partial form only. As I am totally new to ASP.Net and MVC, I have been searching for the last 2 days but still I havn't found the answer.
Here is the serialized data :
__RequestVerificationToken=67iIFQEtEOgXTrApYDPuoTC7EvLs0cXYOgzdFrnUwUQpPac5NsxOgo6q-YM5Q3qH9Votloa1sYrzahLoYuKLLetghrk8pNt9s2Y80KVobl41&BomId=0&ItemId=1&BomName=&ItemName=item+1001&Quantity=0&IsActive=false&IsDefault=false&WithOperations=false&CurrencyId=1&__RequestVerificationToken=FXc2NYDlDaNwuqOnZQNIYOonE8_8qk61xaxM5M92cOSz938KA5CRzAvBUSlP_HnM1g4iozC56r1Kh6VLXMex6l97SY3dxNhGQfv2oeOVHTM1&BomId=0&BomMaterialId=0&ItemId=5&Items.SellingRate=33&Rate=452&Amount=14916&Scrap=12
I just want this :
__RequestVerificationToken=FXc2NYDlDaNwuqOnZQNIYOonE8_8qk61xaxM5M92cOSz938KA5CRzAvBUSlP_HnM1g4iozC56r1Kh6VLXMex6l97SY3dxNhGQfv2oeOVHTM1&BomId=0&BomMaterialId=0&ItemId=5&Items.SellingRate=33&Rate=452&Amount=14916&Scrap=12
If anything else required then I will provide.
This is the main form in which I have called the partial view. For brevity I have cut it short.
@model ERPFynWeb.Infrastructure.ViewModels.BomViewModel
....
@Scripts.Render("~/bundles/jquery")
@using(Html.BeginForm("SaveBom" , "Bom",new {@id="BomForm"}))
{
@Html.AntiForgeryToken()
@Html.HiddenFor(m => m.BomId)
@Html.LabelFor(m => m.ItemId)
@Html.DropDownListFor(m => m.ItemId , new SelectList(Model.Items , "ItemId", "ItemCode") , "Select item code" , new { @id = "ddlItem" , @class = "form-control" })
@Html.ValidationMessageFor(m => m.ItemId)
<div id="addMaterials" style="border: 1px solid lightgray; margin-bottom: 20px; padding-bottom: 5px;">
@Html.Partial("_MaterialsPartialForm",Model.BommaterialsViewModel)
</div>
<button type="submit" value="submit" class="btn btn-primary" >Save</button>
}
This is the partial-view full code :
@model ERPFynWeb.Infrastructure.ViewModels.BomMaterialsViewModel
@using(Ajax.BeginForm("AddMaterials" , "Bom" ,null,
new AjaxOptions { HttpMethod = "POST" } , new { @id = "ajaxBomMaterials" }))
{
@Html.AntiForgeryToken()
@Html.HiddenFor(m => m.BomId)
@Html.HiddenFor(m => m.BomMaterialId)
@Html.LabelFor(m => m.ItemId)
@Html.DropDownListFor(m => m.ItemId , new SelectList(Model.ItemsList , "ItemId" , "ItemCode") , "Select item" , new { @id = "itemDdl" , @class = "form-control" })
@Html.ValidationMessageFor(m => m.ItemId)
@Html.LabelFor(m => m.Quantity)
@Html.TextBoxFor(m => m.Items.SellingRate , new { @id = "qtyBox" , @class = "form-control" })
.... // more inputs
<button onclick="AddMaterialAjax();" type="button">Save it</button>
}
And this is the jquery which I am using on partial view.
function AddMaterialAjax() {
var data = $("#ajaxBomMaterials").serialize();
$.ajax({
url: "@Url.Action("AddMaterials","Bom")",
type: "POST",
data: data,
success: function () {
alert("Materials added");
},
error: function () {
alert("Materials cannot be added");
}
});
}
That's all.