ASP.NET MVC Model Binder возвращает нулевой объект

у меня возникает проблема, когда каждый раз, когда я помещаю форму обратно в [HttpPost] версию действия моего контроллера, ModelBinder возвращает нулевой объект. Я не могу понять почему. Если я поменяю подпись на использование FormCollection, то увижу, что все правильные ключи установлены. Кто-нибудь может помочь мне определить, что здесь не так, потому что я не могу это заметить.

Вот модели для работы с моими взглядами

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<SelectListItem> Manufacturers { get; set; } 
}

, которые я использую в моем контроллере так:

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();
}

И взгляд выглядит так:

@model TMDM.Models.DeviceCreateViewModel

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>

        <div class="editor-label">
            @Html.LabelFor(model => model.ManufacturerId)
        </div>
        <div class="editor-field">
            @Html.DropDownList("ManufacturerId", Model.Manufacturers)
            @Html.ValidationMessageFor(model => model.ManufacturerId)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Model)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Model)
            @Html.ValidationMessageFor(model => model.Model)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.CodeName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.CodeName)
            @Html.ValidationMessageFor(model => model.CodeName)
        </div>

        <p>
            <input type="submit" value="Save" class="medium green awesome" />
            @Html.ActionLink("Cancel", "Index", "Device", null, new { @class="medium black awesome" })
        </p>
    </fieldset> }
11
задан Chris 22 September 2011 в 02:25
поделиться