Asp.net

ASP.NET MVC 模型綁定器返回空對象

  • November 3, 2015

我遇到一個問題,每次我將表單發布回[HttpPost]控制器操作的版本時,ModelBinder 返回一個空對象。我不知道為什麼。如果我將簽名更改為使用 aFormCollection代替,我可以看到所有正確的密鑰都已設置。有人可以幫我指出這裡出了什麼問題,因為我無法發現它。

以下是使用我的視圖的模型

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> }

Model問題是類中命名的屬性和動作中命名的DeviceModel變數之間存在名稱衝突。名稱衝突導致失敗,因為它試圖將 Model 屬性綁定到 DeviceModel 類。model``Create``DefaultModelBinder

將 Create 操作中的變數名稱更改為deviceModel,它將正確綁定。

引用自:https://stackoverflow.com/questions/7508896