【问题标题】:ASP.NET MVC View Model not binding on HTTP Post with DropDownListASP.NET MVC 视图模型未绑定到带有 DropDownList 的 HTTP Post
【发布时间】:2013-02-20 02:11:04
【问题描述】:

我遇到一个问题,当我发布到控制器时,我失去了绑定,并且我的视图模型中的所有内容都是 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>                      
}

视图模型:

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?我可以在使用 Chromes 工具时看到,在帖子中为字符串和整数发布了值...作为一种解决方法,我刚刚完成了以下操作以使代码正常工作:

解决方法控制器:

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

谢谢

【问题讨论】:

    标签: asp.net-mvc-3 razor http-post asp.net-mvc-viewmodel


    【解决方案1】:

    要在 View 中建模绑定,您应该使用 strongly-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" })
    

    【讨论】:

    • 非常感谢。那行得通……我已经挣扎了3个小时。 :-(
    【解决方案2】:

    看起来这可能是因为this question 中提到的属性和变量之间的差异。将{ get; set; } 添加到要绑定的所有变量中。如该问题所述,我认为这是因为默认模型绑定器使用反射的方式。

    过去我发现实际获取 ASP.NET MVC 源代码很有用,这样我就可以调试它并查看发生了什么。

    【讨论】:

    • 这让我发疯了。有时您看不到眼前的事物。
    • 此修复工作完美。这也让我发疯了,因为我在监控请求时清楚地看到了发布的数据。
    【解决方案3】:

    view model 为NULL还有另一个原因:

    • 动作参数中模型的名称与视图模型的属性之一相同


    例子:

    型号:

    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++;
       //...
    }
    


    提示: 这里同名表示不区分大小写(newBookNewBook 相同)

    提示:在 MVC3 和 MVC4 中面临。

    【讨论】:

    • 我刚刚被这个抓住了...好像很多隐藏的保留字/规则带有动作参数名称...前几天我想有个参数叫'action ' 这打破了我的路由!在幕后的某个地方,我假设它正在创建一个匿名对象,例如路由值匿名对象,它将这些参数转换为该对象的属性,所以如果你使用“控制器”、“动作”等名称,它就会搞砸。
    猜你喜欢
    • 2014-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多