邮政形式与jqGrid的在ASP.Net MVC 3 [英] Post Form with jQgrid in ASP.Net MVC 3
问题描述
我想我的后搜索表单到一个控制器和JSON结果加载到jqGrid的。但我总是在我的控制器越来越空。
公共类SearchViewModel
{
公共字符串名称{;设置;}
公共字符串位置{获取;设置;}
公共字符串雇员{获取;设置;}
}@model UI.ViewModel.SearchViewModel< DIV ID =搜索面板>
@ {Html.EnableClientValidation();}
@using(Html.BeginForm(搜索,家,
FormMethod.Post,新{ID =搜索表单}))
{
@ Html.AntiForgeryToken()
< DIV CLASS =formfield容器>
<标签>
<跨度类=列>名称:LT; / SPAN>
@ Html.TextBoxFor(M = GT; m.Name)
< /标签>
< / DIV>
< DIV CLASS =formfield容器>
<标签>
<跨度类=列>的Emp编号:LT; / SPAN>
@ Html.TextBoxFor(M = GT; m.EmployeeId)
< /标签>
< / DIV>
< DIV CLASS =formfield容器>
<标签>
<跨度类=列>位置:其中,/ SPAN>
@ Html.TextBoxFor(M = GT; m.Location)
< /标签>
< / DIV>
}
这是我的jqGrid code
的jQuery(#清单)。jqGrid的({
//开始
网址:'主页/搜索',
数据类型:JSON
发布数据: {
视图模型:函数(){返回$(#搜索表)序列化(); }
},
MTYPE:POST,
//等其他配置去
});
下面是我的控制器code
[HttpPost]
公共JsonResult搜索(SearchViewModel视图模型)
{
//视图模型参数总是空
VAR的SearchResult = //获取从数据库记录和preparing结构的jqGrid
//请求[POSTDATA」]给我的窗体域,它的价值JSON数组刺痛
JSON(信息搜索结果); }
我也试过下面这篇文章。 jqGrid的负载发布自定义数据。但SearchViewModel对象为null。
但我可以看到它在Requst [「POSTDATA]值作为一个JSON字符串我
表单字段名称和值。这意味着我的形式成功地发布与数据作为JSON刺痛服务器。 我怎样才能直接拿到那些JSON
字符串直接强类型的模型
块引用>但是我可以看到Request.Params [视图模型]包含表单元素细节
任何一个可以帮助让我的形式张贴视图模型?或者我需要
写我自己的自定义模型绑定?的
块引用>解决方案我们使用的是一个小功能序列化的形式进入了一个对象:
$。fn.serializeObject =功能()
{
VAR O = {};
变种一个= this.serializeArray();
$。每个(一,函数(){
如果(O [this.name]!==未定义){
如果(O![this.name] .push){
Ø[this.name] = [问题o [this.name]];
}
Ø[this.name] .push(THIS.VALUE ||'');
}其他{
Ø[this.name = || THIS.VALUE '';
}
});
返回O;
};然后在jqGrid的:
的jQuery(#清单)。jqGrid的({
...
beforeRequest:GridOnBeforeRequest,
...
});功能GridOnBeforeRequest()
{
VAR数据= $(#搜索表)serializeObject()。
$(#清单)的jqGrid('setGridParam',{POSTDATA:数据})。
}然后刷新网格:
$(#清单),触发('reloadGrid')。
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 ?
解决方案We are using a small function to serialize the form into an object for that:
$.fn.serializeObject = function() { var o = {}; var a = this.serializeArray(); $.each(a, function() { if (o[this.name] !== undefined) { if (!o[this.name].push) { o[this.name] = [o[this.name]]; } o[this.name].push(this.value || ''); } else { o[this.name] = this.value || ''; } }); return o; };
And then in the jQgrid:
jQuery("#list").jqGrid({ ... beforeRequest: GridOnBeforeRequest, ... }); function GridOnBeforeRequest() { var data = $("#search-form").serializeObject(); $("#list").jqGrid('setGridParam', { postData: data }); }
And then to refresh the grid:
$("#list").trigger('reloadGrid');
这篇关于邮政形式与jqGrid的在ASP.Net MVC 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!