我正在使用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;
}
}
当我在网格中按下transmitt时,我得到一个发送到控制器方法的空对象,不会发生模型绑定。我做错了什么?
答案 0 :(得分:0)
我发现了问题所在,我还没有发布所有代码来展示它。 对于我的类CellEditingViewModel我正在使用
using System.Web.Http.ModelBinding;
哪个错了。我应该一直在使用
using System.Web.Mvc;