2013-03-05 96 views
13

我有一个问题,当我发布到控制器时,我失去了绑定,并且我的视图模型中的所有内容都是NULL。这里是我使用的代码:ASP.NET MVC视图模型不绑定在HTTP Post with DropDownList

查看:

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

为什么我的viewmodel中的所有内容都是NULL?我可以使用在博文的值被公布为字符串和整数铬合金工具看......作为一种解决我刚才做了以下获得代码工作:

解决方法控制器:

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

感谢

回答

39

貌似在this question指出这可能是因为属性和变量之间的差异。将{ get; set; }添加到您想要绑定的所有变量。正如在这个问题中指出的,我认为这是因为默认模型联编程序使用反射的方式。

我发现它实际上抓住了ASP.NET MVC源代码,因此我可以调试它并查看正在发生的事情。

+2

这是驾驶我NUTS。有时你看不到眼前的事情。 – 2015-06-26 18:26:55

+0

这个修复工作完美。这也让我感到非常紧张,因为我在监视请求时清楚地看到了发布的数据。 – 2016-10-17 18:36:14

2

要查看模型绑定,你应该使用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" }) 
33

还有另一个原因为视图模型是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++; 
    //... 
} 


提示:这里相同名字的意思是不区分大小写( newBook是相同 NewBook

提示:面对在MVC3中d MVC4。

+2

我刚刚被这一个发现......似乎是很多带有动作参数名称的隐藏保留字/规则......前几天我想要一个名为'action'的参数,它打破了我的路由!在引擎盖下的某个地方,我认为它会创建一个匿名对象,如路由值匿名对象,将这些参数转换为该对象的属性,所以如果您使用“控制器”,“操作”等名称,它会将事情搞砸。 – 2014-05-29 10:18:55

+1

也见于MVC5 – 2014-06-13 15:26:19