我正在使用asp.net mvc中的Kendo UI网格,其中我有一个包含多个属性的模型。其中一个属性(Id)是Guid。每当我尝试在网格中创建一个新行时,输入所有细节(Id由后端服务器自动生成)并按“更新”,我收到一条错误消息“需要Id字段”。有办法解决这个问题吗?
我怀疑Guid-type是造成麻烦的人。 格:
@(Html.Kendo().Grid<ToolModel>()
.Name("grid")
.Columns(columns =>
{
columns.Bound(p => p.Displace);
columns.Bound(p => p.FishingNectOd);
columns.Bound(p => p.Length);
columns.Bound(p => p.Model);
columns.Bound(p => p.Name);
columns.Bound(p => p.Supplier).Width(150);
columns.Bound(p => p.TagId);
columns.Bound(p => p.ToolOd);
columns.Bound(p => p.Type);
columns.Bound(p => p.Weight);
columns.Command(command => { command.Edit(); command.Destroy(); }).Width(180);
})
.ToolBar(toolbar => toolbar.Create())
.Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName("ToolPopUpTemplate"))
.Pageable()
.Sortable()
.Scrollable()
.HtmlAttributes(new { style = "height:500px;" })
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(10)
.Events(events => events.Error("error_handler"))
.Model(model => model.Id(toolModel => toolModel.Weight))
.Create(update => update.Action("EditingPopup_Create", "ToolManagement"))
.Read(read => read.Action("EditingPopup_Read", "ToolManagement"))
.Update(update => update.Action("EditingPopup_Update", "ToolManagement"))
.Destroy(destroy => destroy.Action("EditingPopup_Destroy", "ToolManagement"))
))
模板:
@using Services.Models.Tool
@model ToolModel
<div class="editor-label">
@(Html.LabelFor(model => model.Displace))
</div>
<div class="editor-field">
@(Html.TextBoxFor(model => model.Displace))
</div>
<div class="editor-label">
@(Html.LabelFor(model => model.FishingNectOd))
</div>
<div class="editor-field">
@(Html.TextBoxFor(model => model.FishingNectOd))
</div>
<div class="editor-label">
@(Html.LabelFor(model => model.Length))
</div>
<div class="editor-field">
@(Html.TextBoxFor(model => model.Length))
</div>
<div class="editor-label">
@(Html.LabelFor(model => model.Model))
</div>
<div class="editor-field">
@(Html.TextBoxFor(model => model.Model))
</div>
<div class="editor-label">
@(Html.LabelFor(model => model.Name))
</div>
<div class="editor-field">
@(Html.TextBoxFor(model => model.Name))
</div>
<div class="editor-label">
@(Html.LabelFor(model => model.Supplier))
</div>
<div class="editor-field">
@(Html.TextBoxFor(model => model.Supplier))
</div>
<div class="editor-label">
@(Html.LabelFor(model => model.TagId))
</div>
<div class="editor-field">
@(Html.TextBoxFor(model => model.TagId))
</div>
<div class="editor-label">
@(Html.LabelFor(model => model.ToolOd))
</div>
<div class="editor-field">
@(Html.TextBoxFor(model => model.ToolOd))
</div>
<div class="editor-label">
@(Html.LabelFor(model => model.Type))
</div>
<div class="editor-field">
@(Html.TextBoxFor(model => model.Type))
</div>
<div class="editor-label">
@(Html.LabelFor(model => model.Weight))
</div>
<div class="editor-field">
@(Html.TextBoxFor(model => model.Weight))
</div>
控制器:
public class ToolManagementController : Controller
{
private static readonly ILog Log = LogManager.GetLogger("ToolManagementController.class");
public ToolServiceClient ToolService { get; set; }
public ToolManagementController()
{
try
{
ToolService = new ToolServiceClient();
}
catch (Exception e)
{
Log.Error(e);
}
}
// GET: ToolManagement
public ActionResult Tools()
{
return View();
}
public ActionResult EditingPopup_Read([DataSourceRequest] DataSourceRequest request)
{
ToolContract[] list = ToolService.GetTools();
return Json(list.ToDataSourceResult(request));
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditingPopup_Create([DataSourceRequest] DataSourceRequest request, ToolModel tool)
{
if (tool != null && ModelState.IsValid)
{
ToolService.CreateTool(new ToolContract
{
Displace = tool.Displace,
FishingNectOD = tool.FishingNectOd,
Length = tool.Length,
Model = tool.Model,
Name = tool.Name,
Supplier = tool.Supplier,
TagId = tool.TagId,
ToolOD = tool.ToolOd,
Type = tool.Type,
Weight = tool.Weight
});
}
return Json(new[] { tool }.ToDataSourceResult(request, ModelState));
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditingPopup_Update([DataSourceRequest] DataSourceRequest request, ToolModel tool)
{
if (tool != null && ModelState.IsValid)
{
ToolService.UpdateTool(new ToolContract
{
Displace = tool.Displace,
FishingNectOD = tool.FishingNectOd,
Length = tool.Length,
Model = tool.Model,
Name = tool.Name,
Supplier = tool.Supplier,
TagId = tool.TagId,
ToolOD = tool.ToolOd,
Type = tool.Type,
Weight = tool.Weight,
Id = tool.Id
});
}
return Json(new[] { tool }.ToDataSourceResult(request, ModelState));
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult EditingPopup_Destroy([DataSourceRequest] DataSourceRequest request, ToolModel tool)
{
if (tool != null)
{
ToolService.DeleteTool(tool.Id);
}
return Json(new[] { tool }.ToDataSourceResult(request, ModelState));
}
}
最后,模型
public class ToolModel
{
private ToolContract _toolContract;
public ToolModel(ToolContract toolContract)
{
ToolContract = toolContract;
}
public ToolModel()
{
}
private ToolContract ToolContract
{
get { return _toolContract ?? (_toolContract = new ToolContract()); }
set { _toolContract = value; }
}
public double? Displace
{
get { return ToolContract.Displace; }
set { ToolContract.Displace = value; }
}
public double? FishingNectOd
{
get { return ToolContract.FishingNectOD; }
set { ToolContract.FishingNectOD = value; }
}
[ScaffoldColumn(false)]
public Guid Id
{
get { return ToolContract.Id; }
set { ToolContract.Id = value; }
}
public double? Length
{
get { return ToolContract.Length; }
set { ToolContract.Length = value; }
}
public string Model
{
get { return ToolContract.Model; }
set { ToolContract.Model = value; }
}
public string Name
{
get { return ToolContract.Name; }
set { ToolContract.Name = value; }
}
public string Supplier
{
get { return ToolContract.Supplier; }
set { ToolContract.Supplier = value; }
}
public string TagId
{
get { return ToolContract.TagId; }
set { ToolContract.Supplier = value; }
}
[Display(Name = "Outer Diameter")]
public double? ToolOd
{
get { return ToolContract.ToolOD; }
set { ToolContract.ToolOD = value; }
}
public string Type
{
get { return ToolContract.Type; }
set { ToolContract.Type = value; }
}
public double? Weight
{
get { return ToolContract.Weight; }
set { ToolContract.Weight = value; }
}
}
答案 0 :(得分:5)
通过向网格中的ID字段添加默认值来解决:
@(Html.Kendo().Grid<ToolModel>()
.Name("grid")
.Columns(columns =>
{
columns.Bound(p => p.Id);
columns.Bound(p => p.Displace);
columns.Bound(p => p.FishingNeckOd);
columns.Bound(p => p.Length);
columns.Bound(p => p.Model);
columns.Bound(p => p.Name);
columns.Bound(p => p.Supplier).Width(150);
columns.Bound(p => p.TagId);
columns.Bound(p => p.ToolOd);
columns.Bound(p => p.Type);
columns.Bound(p => p.Weight);
columns.Command(command => { command.Edit(); command.Destroy(); }).Width(180);
})
.ToolBar(toolbar => toolbar.Create())
.Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName("ToolPopUpTemplate"))
.Pageable()
.Sortable()
.Scrollable()
.HtmlAttributes(new { style = "height:500px;" })
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(10)
.Events(events => events.Error("error_handler"))
.Model(model =>
{
model.Id(toolModel => toolModel.Id);
model.Field(id => id.Id).DefaultValue(Guid.NewGuid());
} )
.Create(update => update.Action("EditingPopup_Create", "ToolManagement"))
.Read(read => read.Action("EditingPopup_Read", "ToolManagement"))
.Update(update => update.Action("EditingPopup_Update", "ToolManagement"))
.Destroy(destroy => destroy.Action("EditingPopup_Destroy", "ToolManagement"))
))