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

前端之家收集整理的这篇文章主要介绍了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源代码,以便我可以调试它,看看发生了什么.

原文链接:https://www.f2er.com/aspnet/250077.html

猜你在找的asp.Net相关文章