hallordylo hallordylo - 2 months ago 11
ASP.NET (C#) Question

Kendo DropDownListFor not s ending info to controller

This is baffling. In one of my MVC models,

Project
, I have two properties which both save the IDs for other models, like so.

public class Project
{
...
public int PlatformID { get; set; }
public int DepartmentID { get; set; }
...
}


I use a custom editor in which I edit these fields with a kendo dropdown, using their respective names and IDs as text and value fields.

<div class="col-md-11 details-item">
@Html.LabelFor(m => m.PlatformID, new { @class = "col-md-4 details-label" })
@(Html.Kendo().DropDownListFor(m => m.PlatformID)
.Name("PlatformID")
.DataValueField("ID")
.DataTextField("PlatformName")
.BindTo((System.Collections.IEnumerable)ViewData["Platforms"])
.HtmlAttributes(new { @class = "col-md-7 details-editor" })
)
</div>
<div class="col-md-11 details-item">
@Html.LabelFor(m => m.DepartmentID, new { @class = "col-md-4 details-label" })
@(Html.Kendo().DropDownListFor(m => m.DepartmentID)
.Name("DepartmentID")
.DataValueField("ID")
.DataTextField("Name")
.BindTo((System.Collections.IEnumerable)ViewData["Departments"])
.HtmlAttributes(new { @class = "col-md-7 details-editor" })
)
</div>


What's crazy is that
PlatformID
saves perfectly, but
DepartmentID
does not even get sent to the controller. If
DepartmentID
is the only thing I change while editing, it does not even register a change has been made. Otherwise, it gets sent as default 0.

The
Department
class is insanely simple.

public class Department
{
[Key]
public int ID { get; set; }
[DisplayName("Department Name")]
public string Name { get; set; }
}


PlatformID
is essentially identical. Maybe I'm missing something stupid, or maybe there is evil afoot. Thanks for any help you can provide!

Answer

Figured out the answer - turns out having a DropDown in an editor template does not work if the item is nullable! So I just changed public int? DepartmentIDto public int DepartmentIDin my Project model, and life was good again.