I have an ajax grid using popup edit mode and kendo dropdownlists in the popup. On the post to the controller the dropdownlist value is not bound to the model property if the model property is a nullable int. If I use the regular mvc dropdownlist the value posts fine. I also have a kendo dropdownlist of states (text and value are both string) and that posts fine to the model property of string.
I am using the internal build from 8/31.
The Grid:
Model's editor template
The Model:
Controller:
I am using the internal build from 8/31.
The Grid:
@(Html.Kendo().Grid<
Contact
>()
.Name("ContactGrid")
.Columns(columns =>
{
columns.Bound(x => x.LastName).Title("Last Name");
columns.Bound(x => x.FirstName).Title("First Name");
columns.Command(command => { command.Edit(); command.Destroy(); }).Title("Actions").Width(200);
})
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(100)
.Model(model => model.Id(p => p.Id))
.Read("_Read", "Clients")
.Update("_Update", "Clients")
.Create("_Create", "Clients")
.Destroy("_Destroy", "Clients")
)
.ToolBar(toolbar => toolbar.Create().Text("Add New Client"))
.Editable(editable => editable.Mode(GridEditMode.PopUp).DisplayDeleteConfirmation(true))
.Sortable()
.Scrollable(scrollable => scrollable.Virtual(true).Height(280))
.Events(events => events.Edit("edit"))
)
Model's editor template
<
div
class
=
"mvceditor-label"
>
@Html.LabelFor(x => x.State)
</
div
>
<
div
class
=
"mvceditor-field"
>
@Html.Kendo().DropDownListFor(x => x.State).BindTo(new SelectList(@ViewBag.States, "Abbreviation", "Name"))
</
div
>
<
div
class
=
"mvceditor-label"
>
@Html.LabelFor(x => x.LabId)
</
div
>
<
div
class
=
"mvceditor-field"
>
@Html.DropDownListFor(x => x.LabId, new SelectList(@ViewBag.Labs, "Id", "Description"))
</
div
>
<
div
class
=
"mvceditor-label"
>
@Html.LabelFor(x => x.TierId)
</
div
>
<
div
class
=
"mvceditor-field"
>
@Html.Kendo().DropDownListFor(x => x.TierId).BindTo(new SelectList(@ViewBag.Tiers, "Id", "Description"))
@Html.ValidationMessageFor(x => x.TierId)
</
div
>
The Model:
public class Contact
{
public int? Id { get; set; }
[Required(ErrorMessage="Required")]
[DisplayName("First Name *")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Required")]
[DisplayName("Last Name *")]
public string LastName { get; set; }
[Required(ErrorMessage = "Required")]
[DisplayName("User Tier *")]
public int? TierId { get; set; }
public string State { get; set; }
[DisplayName("Default Lab")]
public int? LabId { get; set; }
public string LastUpdateBy { get; set; }
}
Controller:
public ActionResult Index()
{
SetupLists();
return View();
}
public ActionResult _Read([DataSourceRequest] DataSourceRequest request)
{
return Json(contactsService.Get().ToDataSourceResult(request));
}
[HttpPost]
public ActionResult _Create([DataSourceRequest] DataSourceRequest request, Contact contact)
{
if (ModelState.IsValid)
{
contact.LastUpdateBy = User.Identity.Name;
contactsService.Create(contact);
}
return Json(new[] { contact }.ToDataSourceResult(request, ModelState));
}
[HttpPost]
public ActionResult _Update([DataSourceRequest] DataSourceRequest request, Contact contact)
{
if (ModelState.IsValid)
{
contact.LastUpdateBy = User.Identity.Name;
contactsService.Update(contact);
}
return Json(new[] { contact }.ToDataSourceResult(request, ModelState));
}
private void SetupLists()
{
IList<
Lab
> labs = labsService.Get();
labs.Insert(0, new Lab { Description = "" });
IList<
Tier
> tiers = tiersService.Get();
tiers.Insert(0, new Tier { Description = "" });
IList<
Institution
> institutions = institutionsService.Get();
institutions.Insert(0, new Institution { Description = "" });
IList<
Contact
> contacts = contactsService.Get();
contacts.Insert(0, new Contact { FullName = "" });
IList<
Grant
> grants = grantsService.Get();
grants.Insert(0, new Grant { Description = "" });
IList<
State
> states = statesService.Get();
states.Insert(0, new State { Name = "" });
ViewBag.Labs = labs;
ViewBag.Tiers = tiers;
ViewBag.Institutions = institutions;
ViewBag.Contacts = contacts;
ViewBag.Grants = grants;
ViewBag.States = states;
}