我終於得到了這個可行。關鍵是要麼把這個
elemPhoneType.select(0); //Re-set the list as non-selection
或取消註釋掉這在JS功能OnIndexChangedPhoneTypeList(),但從來沒有啓用這兩個編碼。
//keep the grid in edit-mode
//var grid = $("#PhonesSubGrid").data("kendoGrid");
//grid.one("dataBinding", function (e) {
// e.preventDefault();
//});
的視圖:
@using Kendo.Mvc.UI;
@using Telerik.OpenAccess;
@model IEnumerable<mdc.Models.ViewModels.ContactPhoneVM>
@{
var actionValidatePhoneType = Url.Action("ValidatePhoneTypeExists", "Locations");
}
@(Html.Kendo().Grid(Model)
.Name("PhonesSubGrid")
.HtmlAttributes(new { style = "text-align:left; max-width: 776px;" })
.Columns(columns =>
{
columns.Bound(p => p.SITE_NUM).Visible(false);
columns.Bound(p => p.CONTACT_ID).Visible(false);
columns.Bound(p => p.ddlPhoneType.TypeName).EditorTemplateName("PhoneTypeDropDownEditor").Title("Phone Type")
.ClientTemplate("#= ddlPhoneType.TypeName #");
columns.Bound(p => p.PHONE_NUMBER).Title("Number");
columns.Bound(p => p.PHONE_EXT).Title("Extension");
columns.Command(c => { c.Edit(); c.Destroy(); });
})
.Pageable(pager => pager
.Input(true)
.Numeric(true)
.Info(true)
.PreviousNext(true)
.Refresh(true)
.PageSizes(true))
.Sortable()
.Scrollable()
.Selectable()
.Filterable()
.ToolBar(t => { t.Create(); })
.Events(events => events.Edit("SubGrid_OnRowEditing"))
.DataSource(
dataSource => dataSource
.Ajax()
.Batch(false)
.ServerOperation(false)
.Model(m =>
{
m.Id(p => p.ID);
m.Field(p => p.CONTACT_ID).DefaultValue(1);
m.Field(p => p.SITE_NUM)
.DefaultValue(mdc.LocationContext.LocationID);
m.Field(p => p.PHONE_NUMBER).Editable(true);
m.Field(p => p.PHONE_EXT).Editable(true);
m.Field(p => p.ddlPhoneType).Editable(true)
.DefaultValue(new mdc.Models.ViewModels.PhoneTypeListItemModel { TypeID = 0, TypeName = "Select a phone type" });
})
.Create(update => update.Action("CreateContactPhoneRecord", "Locations"))
.Update(update => update.Action("UpdateContactPhoneRecord", "Locations"))
.Destroy(update => update.Action("DeleteContactPhoneRecord", "Locations"))
)
)
的下拉列表編輯模板:
@(Html.Kendo().DropDownList()
.Name("ddlPhoneType")
.HtmlAttributes(new { required = true })
.OptionLabel("Select a phone type")
.DataValueField("TypeID")
.DataTextField("TypeName")
.BindTo((System.Collections.IEnumerable)ViewData["phone_type_list"])
.Events(events => events.Change("OnIndexChangedPhoneTypeList"))
)
的客戶端腳本:
<script type="text/javascript">
function OnIndexChangedPhoneTypeList() {
var elemPhoneType = $("#ddlPhoneType").data("kendoDropDownList");
//var inputVal = document.getElementById("phoneTypeValue");
//inputVal.value = elemPhoneType.options.dataSource[elemPhoneType.selectedIndex - 1].TypeID
if (elemPhoneType != null && elemPhoneType.selectedIndex > 0) {
var selPhoneTypeId = elemPhoneType.options.dataSource[elemPhoneType.selectedIndex - 1].TypeID;
$.ajax({
url: '@actionValidatePhoneType',
type: "post",
data: {
ContactID: selectedContactId, PhoneType: selPhoneTypeId
},
dataType: "json",
success: function (result) {
if (result == 'false' || result == false)
{
alert('A phone type may only be used once for a contact. Please choose a different phone type.');
elemPhoneType.select(0); //Re-set the list as non-selection
//keep the grid in edit-mode
//var grid = $("#PhonesSubGrid").data("kendoGrid");
//grid.one("dataBinding", function (e) {
// e.preventDefault();
//});
return false;
}
},
error: function (xhr, status) {
alert('Validating phone type failed. Error: ' + xhr.responseText);
}
});
}
}
</script>
控制器:
public partial class LocationsController : Controller
{
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult CreateContactPhoneRecord([DataSourceRequest] DataSourceRequest request, ContactPhoneVM phone)
{
bool result = false;
if (ModelState.IsValid)
{
try
{
result = _contactsRepository.AddPhone(phone.SITE_NUM, phone.CONTACT_ID, phone.PHONE_TYPE, phone.PHONE_NUMBER, phone.PHONE_EXT, "");
}
catch (Exception ex)
{
string msg = ex.Message;
}
}
return Json(new[] { phone }.ToDataSourceResult(request, ModelState));
}
[AcceptVerbs(HttpVerbs.Post)]
public string ValidatePhoneTypeExists(long ContactID, long PhoneType)
{
long _siteId = mdc.LocationContextCompile.LocationID;
List<long> phonetypeids = new List<long>();
List<ContactPhoneVM> phones = _contactsRepository.GetContactPhones(ContactID, _siteId);
foreach (ContactPhoneVM pvm in phones)
{
phonetypeids.Add(pvm.PHONE_TYPE);
}
if (phonetypeids.Contains(PhoneType))
return "false";
else
return "true";
}
}