17

コントローラに投稿するとバインディングが失われ、ビューモデルのすべてがNULLになるという問題があります。これが私が使用しているコードです:

意見:

@model ArticleCategoryVm

@using (@Html.BeginForm())
{
    @Html.HiddenFor(model => model.LanguageIdDisplayed)

    <label>Name: <span class="label label-important">Required</span></label>
    @Html.HmlValidationFor(model => model.CategoryName)
    @Html.TextBoxFor(model => model.CategoryName, new { maxlength = "255", placeholder = "Category Name", @class = "input-xlarge-fluid" })                  
    <span class="help-block">The is the name of the category and how it appears on the site.</span>

    <label>Language: <span class="label label-important">Required</span></label>
    @Html.HmlValidationFor(model => model.LanguageId)
    @Html.DropDownList("LanguageId", new SelectList(@Model.Languages, "Value", "Text"), "Select language", new { @class="input-xlarge-fluid" })
    <span class="help-block">This will be the language that the category is set too.</span>

    <label>Parent:</label>
    @Html.HmlValidationFor(model => model.CategoryParentId)
    @Html.DropDownList("CategoryParentId", new SelectList(@Model.CategoryParents, "Value", "Text"), new { @class = "input-xlarge-fluid" })
    <span class="help-block">Allows you to group the category under another existing category.</span>

    <label>Moderator Email: <span class="label label-important">Required</span></label>
    @Html.HmlValidationFor(model => model.ModeratorEmail)
    @Html.TextBoxFor(model => model.ModeratorEmail, new { maxlength = "255", placeholder = "Email Address", @class = "input-xlarge-fluid" })
    <span class="help-block">This is the moderator email for articles in this category.</span>

    <button type="submit" class="btn btn-duadua btn-small"><i class="icon-ok-3"></i> Add New Category</button>                      
}

ViewModel:

public class ArticleCategoryVm : BaseLayoutVm
{
    public int LanguageIdDisplayed;
    public List<ArticleCategoryItemVm> ArticleCategoryItemVms { get; set; }

    // Add Category Fields
    [Required]
    public string CategoryName;

    [EmailAttribute]
    [Required]
    public string ModeratorEmail;

    [Required]
    public int LanguageId;

    public int CategoryParentId;

    public List<SelectListItem> Languages { get; set; }
    public List<SelectListItem> CategoryParents { get; set; }
}

コントローラ:

[HttpPost]
public ActionResult Categories(ArticleCategoryVm vm)
{
   // Code here
   if (ModelState.IsValid)
   {
   }

   ReDrawDropDowns(vm);
   return View(vm)
}

ビューモデルのすべてがNULLになるのはなぜですか?Chromeツールを使用すると、投稿で文字列と整数の値が投稿されていることがわかります...回避策として、コードを機能させるために次のことを実行しました。

回避策コントローラー:

[HttpPost]
public ActionResult Categories(int LanguageIdDisplayed, string CategoryName, string ModeratorEmail, int LanguageId, int CategoryParentId)
{
    var vm = new ArticleCategoryVm()
    {
        CategoryName = CategoryName,
        LanguageIdDisplayed = LanguageIdDisplayed,
        ModeratorEmail = ModeratorEmail,
        LanguageId = LanguageId,
        CategoryParentId = CategoryParentId
    };

    if (ModelState.IsValid)
    {
    }

    ReDrawDropDowns(vm);
    return View(vm)
}

ありがとう

4

3 に答える 3

43

この質問に記載されているように、プロパティと変数の違いが原因である可能性があります。バインドするすべての変数に追加{ get; set; }します。その質問で述べたように、これはデフォルトのモデルバインダーがリフレクションを使用する方法が原因だと思います。

過去に、ASP.NET MVC ソース コードを実際に取得してデバッグし、何が起こっているかを確認できると便利であることがわかりました。

于 2013-03-05T16:48:13.653 に答える
41

ビュー モデルが NULLである別の理由があります。

  • アクション パラメータのモデルの名前は、ビュー モデルのプロパティの 1 つと同じである必要があります


例:

モデル:

public class NewBookPageView{
  int blabla {get;set;}
  Book NewBook {get;set;} //NewBook is the same as newBook in action parameter
}

コントローラ:

[HttpPost]
public ActionResult AddNewBook(NewBookPageView newBook)//newBook is the same as NewBook in view
{
   int temp = newBook.blabla;
   temp++;
   //...
}


ヒント:ここで同じ名前は大文字と小文字を区別しないことを意味します ( newBookは NewBook と同じです)

ヒント: MVC3 および MVC4 に直面しています。

于 2013-08-26T08:48:28.257 に答える
3

ビューでバインディングをモデル化するには、strong-typed-html-helpersを使用する必要があります

交換

@Html.DropDownList("CategoryParentId", new SelectList(@Model.CategoryParents, "Value", "Text"), new { @class = "input-xlarge-fluid" })

に:

@Html.DropDownListFor(model => model.CategoryParentId, new SelectList(@Model.CategoryParents, "Value", "Text"), new { @class = "input-xlarge-fluid" })
于 2013-03-05T15:37:26.467 に答える