I have tried several ways to get the serialize data from serialize() method in controller but in controller all the inputs are showing null values.

I have used razor views so that id and name will appear automatically.

Here is my javascript code..

$("#save-btn").click(function () {
        debugger;
        console.log($("supplierForm").serialize());
            $.ajax({
                type: "POST",
                url: "/Home/AddRecords",
                cache: false,
                data: $("#FrmGrid_rowed3").serialize(),
                success: function (response) {
                    if (response.Success)
                    {                     
                        $('#formdivId').css('display', 'none');
                        $('#footerId').css('margin-top', 205);
                        $('#fbox_rowed3_reset').css('height', 28);
                        $('#fbox_rowed3_search').css('height', 30);
                        LoadJqGrid();
                        $('.spanCss').text("");
                        $('.inputCss').val("");
                        $('.ui-jqgrid-headlink.ui-icon.ui-icon-circle-triangle-n').hide();
                        alert(response.Response);
                    }
                    else
                    {
                        $('alert-msg').text(response.Response);
                    }
                }
            });
          //}
        });

And this is my controller code...

 [HttpPost]
       [ValidateCustomAntiForgeryToken]
        public JsonResult AddRecords(SupplierModel supplier)
        {
            if (ModelState.IsValid)
            {
                Repository repo = new Repository();
                string result = repo.AddorEditData(supplier);
                if (result != "")
                {
                    return Json(new { Success = true, Response = result });
                }
                return Json(new { Success = true, Response = result });
            }           
            else
            {
                return Json(new {Success = false , Response = "Please Fill All the Fields"});
            }

        }

And this is the code for razor view

 @using (Html.BeginForm("AddRecords","Home",FormMethod.Post, new { @id = "FrmGrid_rowed3", @class = "form-horizontal", style = "padding-top:25px" }))
                  {
                      @*<form id="FrmGrid_rowed3" name="supplierForm" class="form-horizontal" style = "padding-top:25px">*@
                       @Html.AntiForgeryToken()

                                <div class="row rowCss">
                                  <div class="col-sm-6" >
                                     <div class="form-group">              
                                        <div class="col-sm-2 labelCss" style="margin-left: 25px;">
                                             <label class="control-label">Company Name:</label>
                                        </div>
                                        <div class="col-sm-4 inputCss" style="height:40px">
                                             @Html.HiddenFor(model=>model.supplierID)
                                             @Html.TextBoxFor(model => model.companyName, new { @class="form-control inputCss" , placeholder = "Enter Company Name" }) 
                                             <div style="width:254px">  
                                                 @Html.ValidationMessageFor(model=>model.companyName,"",new{@class="spanCss" , @id="compName"}) 
                                             </div>
                                        </div>     
                                      </div>           
                                 </div>


                                 <div class="col-sm-6 "> 
                                      <div class="form-group">                 
                                        <div class="col-sm-2 labelCss" style="margin-left:-58px"> 
                                             <label class="control-label">Contact Title:</label>
                                         </div>
                                         <div class="col-sm-4 inputCss" style="height:40px">
                                             @Html.TextBoxFor(model => model.contactTitle, new { @class="form-control inputCss" , placeholder = "Enter Contact Name" })                          <span class="spanCss"></span>
                                             <div style="width:254px">  
                                                 @Html.ValidationMessageFor(model=>model.contactTitle,"",new{@class="spanCss" , @id="contTitle"}) 
                                             </div>
                                         </div> 
                                      </div>              
                                 </div>
                              </div>
                            <div class="row rowCss">
                                 <div class="col-sm-6" style="margin-right:1px">
                                     <div class="form-group">              
                                        <div class="col-sm-2 labelCss">
                                             <label class="control-label">Contact Name:</label>
                                        </div>
                                        <div class="col-sm-4 inputCss" style="height:40px"> 
                                             @Html.TextBoxFor(model => model.contactName, new { @class="form-control inputCss" , placeholder = "Enter Contact Title" })
                                             <div style="width:254px">  
                                                 @Html.ValidationMessageFor(model=>model.contactName,"",new{@class="spanCss" , @id="contName"}) 
                                             </div> 
                                        </div>     
                                      </div>           
                                 </div>


                                 <div class="col-sm-6" style="margin-right:-1px"> 
                                      <div class="form-group">                 
                                        <div class="col-sm-2 labelCss" style="margin-left:-57px"> 
                                             <label class="control-label">Address:</label>
                                         </div>
                                         <div class="col-sm-4 inputCss" style="margin-top:-5px" style="height:40px">
                                               @Html.TextAreaFor(model => model.address,2,5, new { @class="form-control inputCss" , placeholder = "Enter Address",style="height:45px" })
                                             <div style="width:254px">  
                                                 @Html.ValidationMessageFor(model=>model.address,"",new{@class="spanCss" , @id="Address"}) 
                                             </div>
                                        </div> 
                                       </div>              
                                 </div>
                               </div>
                            <div class="row rowCss">
                                 <div class="col-sm-6">
                                     <div class="form-group">              
                                        <div class="col-sm-2 labelCss">
                                             <label class="control-label">City:</label>
                                        </div>
                                        <div class="col-sm-4 inputCss" style="height:40px">
                                               @Html.TextBoxFor(model => model.city, new { @class="form-control inputCss" , placeholder = "Enter City Name" })
                                              <div style="width:254px">  
                                                 @Html.ValidationMessageFor(model=>model.city,"",new{@class="spanCss" , @id="City"}) 
                                             </div>
                                        </div>     
                                      </div>           
                                 </div>

                                 <div class="col-sm-6"> 
                                      <div class="form-group">                 
                                        <div class="col-sm-2 labelCss" style="margin-left:-56px"> 
                                             <label class="control-label">Region:</label>
                                         </div>
                                         <div class="col-sm-4 inputCss" style="height:40px">
                                                @Html.TextBoxFor(model => model.region, new { @class="form-control inputCss" , placeholder = "Enter Region" })
                                             <div style="width:254px">  
                                                @Html.ValidationMessageFor(model=>model.region,"",new{@class="spanCss" , @id="Region"}) 
                                             </div>
                                        </div> 
                                       </div>              
                                 </div>
                                </div>
                            <div class="row rowCss">
                                 <div class="col-sm-6" style="margin-right:1px">
                                     <div class="form-group">              
                                        <div class="col-sm-2 labelCss">
                                             <label class="control-label">Postal Code:</label>
                                        </div>
                                        <div class="col-sm-4 inputCss" style="height:40px">
                                               @Html.TextBoxFor(model => model.postalCode, new { @class="form-control inputCss" , placeholder = "Enter Postal Code" })
                                            <div style="width:254px">
                                                @Html.ValidationMessageFor(model=>model.postalCode,"",new{@class="spanCss" , @id="PostalCode" ,style="width:200px" }) 
                                            </div>   
                                        </div>     
                                      </div>           
                                   </div>

                                  <div class="col-sm-6" style="margin-right:-1px"> 
                                      <div class="form-group" >                 
                                        <div class="col-sm-2 labelCss" style="margin-left:-55px"> 
                                             <label class="control-label">Country:</label>
                                         </div>
                                         <div class="col-sm-4 inputCss" style="height:40px">
                                               @Html.TextBoxFor(model => model.country, new { @class="form-control inputCss" , placeholder = "Enter Country" ,style="width:280px" })
                                               <div style="width:254px">  
                                                 @Html.ValidationMessageFor(model=>model.country,"",new{@class="spanCss" , @id="Country"}) 
                                             </div>
                                        </div> 
                                       </div>              
                                 </div>
                             </div>
                            <div class="row rowCss">
                                 <div class="col-sm-6">
                                     <div class="form-group">              
                                        <div class="col-sm-2 labelCss">
                                             <label class="control-label">Phone:</label>
                                        </div>
                                        <div class="col-sm-4 inputCss" style="height:40px">
                                            @Html.TextBoxFor(model => model.phone, new { @class="form-control inputCss" , placeholder = "Enter Phone Number" })
                                           <div style="width:254px">  
                                                 @Html.ValidationMessageFor(model=>model.phone,"",new{@class="spanCss" , @id="Phone"}) 
                                             </div>
                                        </div>     
                                      </div>           
                                   </div>

                                  <div class="col-sm-6" style="margin-left:-54px"> 
                                      <div class="form-group">                 
                                        <div class="col-sm-2 labelCss" style="margin-left:0px" > 
                                             <label class="control-label">Fax:</label>
                                         </div>
                                         <div class="col-sm-4 inputCss" style="height:40px">
                                                 @Html.TextBoxFor(model => model.fax, new { @class="form-control inputCss" , placeholder = "Enter Fax Number" })
                                                 <div style="width:254px">  
                                                 @Html.ValidationMessageFor(model=>model.fax,"",new{@class="spanCss" , @id="Fax"}) 
                                             </div>
                                        </div> 
                                       </div>              
                                 </div>
                             </div>
                            <div class="row rowCss" style="height:24px">
                                <center>
                                    <input id="save-btn" type="button"  value="Submit" />
                                    <input type="button" value="Cancel" id="cData"  />
                                </center>
                            </div>
                  }

Please suggest any solution as i have tried all the ways to get that data in my cotroller.

Related posts

Recent Viewed