我一直在努力解决这个问题。我有一个剑道网格,网格内有几个下拉列表。我使用外键列将dropdownlist放在网格中。内联编辑中的网格不会将选定的下拉列表值发送到控制器,它会发送旧值。我不太确定会出现什么问题,任何线索都会非常有用。
代码:
查看
@(Html.Kendo().Grid(Model.App_Client_Mapping)
.Name("AppAccountInternalGrid")
.Events(ev => ev.Edit("OnGridEdit"))
.Columns(columns =>
{
columns.Bound(p => p.AccountMappingID).Title("AccountMappingID").Width(130).Hidden(true);
columns.ForeignKey(p => p.Level1ID, (System.Collections.IEnumerable)ViewData["level1"], "Level1ID", "Level1").Title("Level 1").Width(150).HtmlAttributes(new { id = "Level1Dropdown", data_value_primitive = true });
columns.ForeignKey(p => p.Level2ID, (System.Collections.IEnumerable)ViewData["level2"], "Level2ID", "Level2").Title("Level 2").Width(150).HtmlAttributes(new { id = "Level2Dropdown" });
columns.Bound(p => p.ExternalAccount).Title("ExternalAccount").Width(150);
})
.Editable(editable => editable.Mode(GridEditMode.InLine))
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(50)
.Model(model =>
{
model.Id(p => p.AccountMappingID);
model.Field(p => p.AccountMappingID).Editable(false);
model.Field(p => p.Level1ID).DefaultValue(1).Editable(true);
model.Field(p => p.Level1).Editable(true);
model.Field(p => p.Level2ID).DefaultValue(0).Editable(true);
model.Field(p => p.Level2).Editable(true);
}
)
.Update("EditingInline_Update", "AppAccounts")
.ServerOperation(false)
)
)
控制器:
网格从此操作中获取数据。该视图还有其他元素以及网格,因此我绑定了一个视图模型。
public ActionResult AppInternal(int clientid)
{
var result = new AppViewModel();
result.App_Client_Mapping=_unitofwork.AppInternal.Get(clientid);
var level1 = level1list.Select(x => new App_Client_Mapping
{
Level1ID = x.LevelID,
Level1 = x.Level1
}).ToList();
var level2 = level2list.Select(x => new App_Client_Mapping
{
Level2ID = x.Level2ID,
Level2 = x.Level2
}).ToList();
ViewData["level1"] = level1;
ViewData["level2"] = level2;
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditingInline_Update([DataSourceRequest] DataSourceRequest request, App_Client_Mapping mapping)
{
//save the mapping in the database tables
return Json(new[] { mapping }.ToDataSourceResult(request, ModelState));
}
视图模型:
AppViewModel:
public class AppMappingViewModel
{
public IEnumerable<App_Client_Mapping> App_Client_Mapping { get; set; }
//other fields
}
从EF自动生成的app_client_mapping类:
public class app_client_mapping
{
public int AccountMappingID{get;set;}
public int Level1ID{get;set;}
public string Level1{get;set;}
public int Level2ID{get;set;}
public string Level2{get;set;}
public string ExternalAccount{get;set;}
}
将数据集传递给控制器时。仅发送网格中外部本地量(即文本框)的更新值。更新的下拉值不会发送到网格。关于为什么会发生这种情况的任何想法都将是一个巨大的帮助。
谢谢!
答案 0 :(得分:0)
您似乎错过了网格的.Read
操作?
以下是我在网格中的下拉列表。我试图使它与你的代码一样。
控制器:
public IActionResult AppInternal()
{
ViewData["level1"] = _context.level1list.Select(x => new App_Client_Mapping {
Level1ID = x.LevelID,
Level1 = x.Level1
}); // Do not return a .List()
// put more dropdowns as required...
return View();
}
// return the .Read action from your grid
public IActionResult Read([DataSourceRequest] DataSourceRequest request)
{
var result = new AppViewModel();
result.App_Client_Mapping=_unitofwork.AppInternal.Get(clientid);
return Json(result.ToDataSourceResult(request));
}
// Update
public EditingInline_Update([DataSourceRequest] DataSourceRequest request, App_Client_Mapping mapping)
{
if (ModelState.IsValid)
{
// update db with the Level1ID from your dropdown
}
// Return the updated product. Also return any validation errors.
return Json(new[] { mapping }.ToDataSourceResult(request, ModelState));
}
查看:
columns.ForeignKey(p => p.Level1ID, (System.Collections.IEnumerable)ViewData["level1"], "Level1ID", "Level1").Title("Level 1").Width(150); // Don't need to put HtmlAttributes.
// The .Read Action should be
.Read(read => read.Action("Read", "AppAccounts")
外键编辑模板:
@model object
@(Html.Kendo().ComboBoxFor(m => m)
.BindTo((SelectList)ViewData[ViewData.TemplateInfo.GetFullHtmlFieldName("") + "_Data"])
.ValuePrimitive(true)
)
这里有一些代码示例:MVC Demo