我在使用模板添加/编辑时遇到了剑道网格问题。添加/编辑帖子中的对象(ContactEmail)为空,我无法弄清楚为什么,ContactID 有一个值。我在 KendoUI 论坛和此处查看了对类似问题的回复,但没有找到任何可以解决我的问题的方法。
即使没有模板,它也不起作用,因此我的自定义模板似乎不是问题。也没有验证,所以我不明白为什么它不会通过。电子邮件基本上只是电话的副本,只是更改为使用电子邮件对象。
我的视图上有 2 个剑道网格,下面是其中的一个片段。只有电子邮件有问题:
@model DORIS.POCO.Contact
<div id="phones">
<h4>Phones</h4>
<div id="phoneGrid" style="width:500px">
@(Html.Kendo().Grid<DORIS.POCO.ContactPhone>()
.Name("kendoContactPhones")
.Columns(columns =>
{
columns.Bound(m => m.Number).Title("Phone").ClientTemplate("#= getFormattedPhone(Number) #");
columns.Bound(m => m.Category).Title("Type");
columns.Bound(m => m.DescriptionOf).Title("Description");
columns.Bound(m => m.PrimaryPhone).Title("Primary").ClientTemplate("<input type='checkbox' disabled='true' #= PrimaryPhone ? checked='checked' :'' # />");
})
.Sortable()
.Selectable(s => s
.Mode(GridSelectionMode.Single)
.Type(GridSelectionType.Row)
)
.DataSource(d => d
.Ajax()
.Model(model => model.Id(m => m.PhoneID))
.Read(read => read.Action("GetContactPhones", "ContactPhones", new{ id = Model.ContactID }))
.Create(create => create.Action("KendoPhoneAdd", "ContactPhones", new{ id = Model.ContactID }))
.Update(update => update.Action("KendoPhoneEdit", "ContactPhones"))
.Destroy(destroy => destroy.Action("KendoPhoneDelete", "ContactPhones"))
.Sort(s => s.Add(m => m.PrimaryPhone).Descending())
)
.AutoBind(true)
.ToolBar(toolBar => toolBar.Template("<a id='addPhone' class='k-button k-button-icontext k-grid-add'><span class='k-icon k-add'></span>Add</a><a id='editPhone' class='k-button k-button-icontext k-grid-edit'><span class='k-icon k-edit'></span>Edit</a><a id='deletePhone' class='k-button k-button-icontext k-grid-delete'><span class='k-icon k-delete'></span>Delete</a>"))
.Editable(e => e.Mode(GridEditMode.PopUp).TemplateName("ContactPhone").Window(w => w.Title("Manage Phone")))
.Events(e => e.DataBound("toggleBacktoTopButton"))
)
</div>
</div>
<div id="emails>">
<h4>Email</h4>
<div id="emailGrid" style="width:500px">
@(Html.Kendo().Grid<DORIS.POCO.ContactEmail>()
.Name("kendoContactEmails")
.Columns(columns =>
{
columns.Bound(m => m.Email).Title("Email");
columns.Bound(m => m.DescriptionOf).Title("Description");
columns.Bound(m => m.PrimaryEmail).Title("Primary").ClientTemplate("<input type='checkbox' disabled='true' #= PrimaryEmail ? checked='checked' :'' # />");
})
.Sortable()
.Selectable(s => s
.Mode(GridSelectionMode.Single)
.Type(GridSelectionType.Row)
)
.DataSource(d => d
.Ajax()
.Model(model => model.Id(m => m.EmailID))
.Read(read => read.Action("GetContactEmails", "ContactEmails", new{ id = Model.ContactID }))
.Create(create => create.Action("KendoEmailAdd", "ContactEmails", new{ id = Model.ContactID }))
.Update(update => update.Action("KendoEmailEdit", "ContactEmails"))
.Destroy(destroy => destroy.Action("KendoEmailDelete", "ContactEmails"))
.Sort(s => s.Add(m => m.PrimaryEmail).Descending())
)
.AutoBind(true)
.ToolBar(toolBar => toolBar.Template("<a id='addEmail' class='k-button k-button-icontext k-grid-add'><span class='k-icon k-add'></span>Add</a><a id='editEmail' class='k-button k-button-icontext k-grid-edit'><span class='k-icon k-edit'></span>Edit</a><a id='deleteEmail' class='k-button k-button-icontext k-grid-delete'><span class='k-icon k-delete'></span>Delete</a>"))
.Editable(e => e.Mode(GridEditMode.PopUp).TemplateName("ContactEmail").Window(w => w.Title("Manage Email")))
.Events(e => e.DataBound("toggleBacktoTopButton"))
)
</div>
下面是我的 ContactEmailsController 的代码,仅限 Add Post 方法:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult KendoEmailAdd([DataSourceRequest] DataSourceRequest request, ContactEmail email, int id)
{
if (email != null)
{
ContactEmail newEmail = new ContactEmail();
newEmail.DescriptionOf = email.DescriptionOf;
newEmail.Email = email.Email;
newEmail.EntityID = id;
newEmail.PrimaryEmail = email.PrimaryEmail;
db.ContactEmails.Add(newEmail);
db.SaveChanges();
}
return Json(new[] { email }.ToDataSourceResult(request, ModelState));
}
这是我的 ContactEmail 类:
public partial class ContactEmail
{
public int EmailID { get; set; }
public int EntityID { get; set; }
public string Email { get; set; }
public string DescriptionOf { get; set; }
public bool PrimaryEmail { get; set; }
public virtual Contact Contact { get; set; }
}
最后,这是我的 ContactEmail 编辑器模板:
@model DORIS.POCO.ContactEmail
@Html.HiddenFor(m => m.EmailID)
@Html.HiddenFor(m => m.EntityID)
<div class="editor-label">
@Html.LabelFor(m => m.Email)
</div>
<div class="editor-field">
@Html.EditorFor(m => m.Email )
@Html.ValidationMessageFor(m => m.Email)
</div>
<div class="editor-label">
@Html.Label("Description")
</div>
<div class="editor-field">
@Html.EditorFor(m => m.DescriptionOf)
@Html.ValidationMessageFor(m => m.DescriptionOf)
</div>
<div class="editor-label">
@Html.Label("Primary Email")
</div>
<div class="editor-field">
@Html.CheckBoxFor(m => m.PrimaryEmail)
@Html.ValidationMessageFor(m => m.PrimaryEmail)
</div>
我已经研究了很长一段时间,包括在互联网上搜索解决方案。也许我看起来太努力了,答案非常简单。
我相信我已经包含了与此问题相关的所有代码,但是如果您认为我遗漏了某些内容,请告诉我,我会添加它。
谢谢!非常感谢任何帮助!