我正在使用 JQGrid 来编辑数据网格。我想将单元格中的数据发送到服务器上进行编辑。所以我的观点看起来像;
@using Lib.Web.Mvc.JQuery.JqGrid
@using Tac.P3.Model
@model ContractType
@{
ViewBag.Title = "List of Contract Types";
var grid = new JqGridHelper<ContractType>(
"ContractTypes",
dataType: JqGridDataTypes.Json,
methodType: JqGridMethodTypes.Post,
pager: true,
rowsNumbers: true,
rowsNumber: 20,
shrinkToFit: true,
cellEditingEnabled: true,
cellEditingSubmitMode: JqGridCellEditingSubmitModes.Remote,
cellEditingUrl:Url.Action("Update", "ContractType"),
sortingName: "ContractTypeLabel", sortingOrder: JqGridSortingOrders.Asc,
url: Url.Action("GridData"),
viewRecords: true)
.Navigator(new JqGridNavigatorOptions()
{
Add = false,
Edit = false,
Delete = false,
Search = false
});
}
<div class="mainPage">
<fieldset>
<legend>Add New Contract Type</legend>
<form class="form-horizontal" method="POST" action="@Url.Action("Add")">
<div class="form-group">
<div class="col-xs-1">@Html.LabelFor(model => model.ContractTypeLabel)</div>
<div class="col-xs-10">@Html.TextBoxFor(model => model.ContractTypeLabel)</div>
</div>
<div class="form-group">
<div class="col-xs-1">@Html.LabelFor(model => model.Description)</div>
<div class="col-xs-10">@Html.TextBoxFor(model => model.Description)</div>
</div>
<div class="form-group">
<div class="col-xs-offset-1 col-xs-10">
<button class="btn btn-primary" id="btnSubmitForm">Add</button>
</div>
</div>
</form>
</fieldset>
<fieldset>
<legend>List of Contract Types</legend>
@Html.Partial("_Messages")
<div id="loadingMessage" class="errorHighlight">Loading list of Contract Types, please wait...</div>
@grid.GetHtml()
</fieldset>
</div>
我在控制器上的方法看起来像
public ActionResult Update(CellEditingViewModel viewModel)
{
var contractType = this.TacUoW.ContractType.GetById(viewModel.Id);
switch (viewModel.PropertyName)
{
case "ContractTypeLabel":
contractType.ContractTypeLabel = viewModel.PropertyValue.ToString();
break;
case "Description":
contractType.Description = viewModel.PropertyValue.ToString();
break;
}
this.TacUoW.ContractType.Update(contractType);
this.TacUoW.SaveChanges();
return this.Json(true);
}
我的视图模型看起来像;
[ModelBinder(typeof(CellEditingViewModelBinder))]
public class CellEditingViewModel
{
public int Id { get; set; }
public string PropertyName { get; set; }
public object PropertyValue { get; set; }
}
我的 ModelBinder 看起来像;
public class CellEditingViewModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var request = controllerContext.HttpContext.Request;
var model = new CellEditingViewModel { Id = Convert.ToInt32(request.Params["ContractTypeId"]) };
if (request.Params.AllKeys.Contains("ContractTypeLabel"))
{
model.PropertyName = "ContractTypeLabel";
model.PropertyValue = request.Params["ContractTypeLabel"];
}
else if (request.Params.AllKeys.Contains("Description"))
{
model.PropertyName = "Description";
model.PropertyValue = request.Params["Description"];
}
return model;
}
}
当我在网格中按下传输时,我得到一个空对象发送到控制器方法,没有发生模型绑定。我究竟做错了什么?