Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC Model Binder returns null object
    text
    copied!<p>I am having a problem where everytime I post a form back to the <code>[HttpPost]</code> version of my controller action, the ModelBinder returns a null object. I can't work out why. If I change the signature to use a <code>FormCollection</code> instead I can see that all the correct keys have been set. Can someone help me pin point what's wrong here, because I can't spot it.</p> <p>Here are the models for working with my views</p> <pre><code>public class DeviceModel { public int Id { get; set; } [Required] [Display(Name = "Manufacturer")] public int ManufacturerId { get; set; } [Required] [Display(Name = "Model")] [StringLength(20)] public string Model { get; set; } [StringLength(50)] [Display(Name = "Name")] public string Name { get; set; } [StringLength(50)] [Display(Name = "CodeName")] public string CodeName { get; set; } public int? ImageId { get; set; } } public class DeviceCreateViewModel : DeviceModel { public IEnumerable&lt;SelectListItem&gt; Manufacturers { get; set; } } </code></pre> <p>Which I use in my controller like so:</p> <pre><code>public ActionResult Create() { DeviceCreateViewModel viewModel = new DeviceCreateViewModel() { Manufacturers = ManufacturerHelper.GetSortedManufacturersDropDownList() }; return View(viewModel); } [HttpPost] public ActionResult Create(DeviceModel model) { // if I check model here it is NULL return View(); } </code></pre> <p>And the view looks like this:</p> <pre><code>@model TMDM.Models.DeviceCreateViewModel &lt;script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"&gt;&lt;/script&gt; @using (Html.BeginForm()) { @Html.ValidationSummary(true) &lt;fieldset&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.ManufacturerId) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.DropDownList("ManufacturerId", Model.Manufacturers) @Html.ValidationMessageFor(model =&gt; model.ManufacturerId) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Model) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.Model) @Html.ValidationMessageFor(model =&gt; model.Model) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.Name) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.Name) @Html.ValidationMessageFor(model =&gt; model.Name) &lt;/div&gt; &lt;div class="editor-label"&gt; @Html.LabelFor(model =&gt; model.CodeName) &lt;/div&gt; &lt;div class="editor-field"&gt; @Html.EditorFor(model =&gt; model.CodeName) @Html.ValidationMessageFor(model =&gt; model.CodeName) &lt;/div&gt; &lt;p&gt; &lt;input type="submit" value="Save" class="medium green awesome" /&gt; @Html.ActionLink("Cancel", "Index", "Device", null, new { @class="medium black awesome" }) &lt;/p&gt; &lt;/fieldset&gt; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload