asp.net-mvc-3 – ASP.NET MVC视图模型不绑定在HTTP Post与DropDownList

我有一个问题,当我发布到控制器,我失去绑定,我的视图模型中的所有内容都是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,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,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?我可以看到使用Chromes工具,在发布中的值正在发布的字符串和int …作为一个工作,我刚刚完成以下操作来获取代码的工作:

解决方案控制器

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

谢谢

解决方法

看起来可能是由于属性和变量之间的区别,如 this question所示.组; }到要绑定的所有变量.正如在这个问题中提到的,我认为这是因为认模型绑定器使用反射的方式.

我发现以前有用的是实际抓住ASP.NET MVC源代码,以便我可以调试它,看看发生了什么.

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...