I tried to post my search form to a controller and load the json result into jQGrid. But i always getting null in my controller.
public class SearchViewModel
{
public string Name{get;set;}
public string Location{get;set;}
public string EmployeeId{get;set;}
}
@model UI.ViewModel.SearchViewModel
<div id="search-panel">
@{Html.EnableClientValidation();}
@using (Html.BeginForm("Search", "Home",
FormMethod.Post, new { id = "search-form" }))
{
@Html.AntiForgeryToken()
<div class="formfield-container">
<label>
<span class="column">Name:</span>
@Html.TextBoxFor(m => m.Name)
</label>
</div>
<div class="formfield-container">
<label>
<span class="column">Emp Id:</span>
@Html.TextBoxFor(m => m.EmployeeId)
</label>
</div>
<div class="formfield-container">
<label>
<span class="column">Location:</span>
@Html.TextBoxFor(m => m.Location)
</label>
</div>
}
This is my jQGrid code
jQuery("#list").jqGrid({
//start
url: 'Home/Search',
datatype: "json",
postData: {
viewModel: function () { return $("#search-form").serialize(); }
},
mtype: "POST",
//etc.. other config goes
});
Here is my controller code
[HttpPost]
public JsonResult Search(SearchViewModel viewModel)
{
//viewModel parameter is always null
var searchResult=//getting records from DB and preparing structure for jQGrid
//Request["PostData"] gives json array sting of my form field and its value
Json(searchResult);
}
I tried following this post also. jQgrid posting custom data on load. But SearchViewModel object is null.
But i can see it in Requst["PostData"] value as a JSON string of my form field name and its value. This mean my form is posting successfully to the server with the data as JSON sting. How can i directly get those JSON string to direct strongly typed model.
But i could see Request.Params["viewModel"] contains the form element details
Any one could help to get my form posted as view model? or Do i need to write my own custom model binder ?