2011-09-22 20 views
11

私はコントローラのアクションの[HttpPost]バージョンにフォームを投稿するたびに、ModelBinderはnullオブジェクトを返します。なぜ私はうまくいかない。代わりにFormCollectionを使用するように署名を変更すると、すべての正しいキーが設定されていることがわかります。私はそれを見つけることができないので、誰かがここで何が間違っているのかを指摘するのを助けることができます。ここで ASP.NET MVCモデルバインダはnullオブジェクトを返します

が私の見解

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

答えて

29

問題は、クラス内のModelという名前のプロパティと名前の衝突があることですDeviceModelCreateアクション内のmodelという名前の変数。名前の衝突により、ModelプロパティをDeviceModelクラスにバインドしようとするため、DefaultModelBinderが失敗します。

[作成]アクションの変数の名前をに変更すると、正しくバインドされます。

+0

ありがとうございました!しかし、HttpPostデータを受け入れる別のコントローラの名前モデルはなぜ機能するのですか? – Chris

+0

そのアクションのモデルにもModelというプロパティがありますか?それがここで失敗した理由は、名前の衝突のためでした。 – counsellorben

+0

オハイオ州申し訳ありません私は実際にあなたが前に言っていたことを誤解しましたが、私は今はっきりしています。ご協力いただきありがとうございます。 – Chris

関連する問題