【发布时间】:2013-01-09 10:39:09
【问题描述】:
我尝试将我的搜索表单发布到控制器并将 json 结果加载到 jQGrid。但我的控制器总是为空。
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>
}
这是我的 jQGrid 代码
jQuery("#list").jqGrid({
//start
url: 'Home/Search',
datatype: "json",
postData: {
viewModel: function () { return $("#search-form").serialize(); }
},
mtype: "POST",
//etc.. other config goes
});
这是我的控制器代码
[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);
}
我也试过关注这篇文章。 jQgrid posting custom data on load。但 SearchViewModel 对象为空。
但我可以在 Requst["PostData"] 值中看到它作为我的 JSON 字符串 表单字段名称及其值。这意味着我的表单已成功发布到服务器,数据为 JSON 字符串。 我怎样才能直接得到那些 JSON 指向强类型模型的字符串。
但我可以看到 Request.Params["viewModel"] 包含表单元素详细信息
任何人都可以帮助将我的表单发布为视图模型?或者我需要 编写我自己的自定义模型绑定器?
【问题讨论】:
-
尝试添加
@Html.HiddenFor(m => m.Location),[在此处查看类似帖子][1] [1]:stackoverflow.com/questions/3866716/what-does-html-hiddenfor-do
标签: asp.net-mvc asp.net-mvc-3 jqgrid