我使用模板添加/编辑时遇到了Kendo Grid的问题。添加/编辑的帖子中的对象(ContactEmail)为null,我无法弄清楚原因,ContactID有一个值。我已经看过对KendoUI论坛和类似问题的回答。在这里,但没有发现任何解决我的问题。
即使没有模板它也不起作用,所以它似乎不是我的自定义模板的问题。也没有验证,所以我不明白为什么它不会通过。电子邮件基本上只是电话的副本,只是更改为使用电子邮件对象。
我的视图中有2个Kendo网格,下面是它们的片段。只有电子邮件有问题:
@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>
我已经看了很长一段时间,包括在互联网上寻找解决方案。也许我看起来太难了,答案非常简单。
我相信我已经包含了与此问题相关的所有代码,但如果您认为我错过了某些内容,请告诉我,我会添加它。
谢谢!非常感谢任何帮助!
答案 0 :(得分:1)
事实证明,在控制器中命名我的变量是一个问题。我将帖子更改为:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult KendoEmailAdd([DataSourceRequest] DataSourceRequest request, ContactEmail newEmail, int id)
{
if (newEmail != null)
{
ContactEmail email = new ContactEmail();
email.DescriptionOf = newEmail.DescriptionOf;
email.Email = newEmail.Email;
email.EntityID = id;
email.PrimaryEmail = newEmail.PrimaryEmail;
db.ContactEmails.Add(email);
db.SaveChanges();
}
return Json(new[] { newEmail }.ToDataSourceResult(request, ModelState));
}
由于我的ContactEmail对象有一个名为“Email”的属性,因此与我名为“email”的变量冲突。
答案 1 :(得分:0)
您可以查看实现自定义弹出编辑器的代码库项目:http://www.kendoui.com/code-library/mvc/grid/custom-popup-editor.aspx