剑道网格自定义添加弹出窗口,控制器Post Action中的值为空
本文关键字:Action Post 控制器 自定义 网格 添加 窗口 | 更新日期: 2023-09-27 18:11:29
我有一个问题与剑道网格使用模板添加/编辑。对象(ContactEmail)在帖子中添加/编辑是空的,我无法弄清楚为什么,ContactID有一个值。我在KendoUI论坛上看过对类似问题的回应。在这里,但没有找到任何解决我的问题。
即使没有模板,它也不能工作,所以它似乎不是我的自定义模板的问题。也没有验证,所以我不明白为什么它不会通过。Email基本上只是Phone的副本,只是改成了Email对象。
我有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的代码,只有添加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>
我一直在看这个相当长一段时间,包括搜索互联网的解决方案。也许我太纠结了,答案其实很简单。
我相信我已经包含了与这个问题相关的所有代码,但是如果你认为我错过了什么让我知道,我会添加它。
谢谢!非常感谢任何帮助!
原来这是控制器中变量命名的问题。我把Post改成:
[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"相冲突。
您可以查看实现自定义弹出式编辑器的代码库项目:http://www.kendoui.com/code-library/mvc/grid/custom-popup-editor.aspx