2013-03-05 67 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