加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-15 23:13:13 所属栏目:asp.Net 来源:网络整理
导读:我有一个问题,当我发布到控制器,我失去绑定,我的视图模型中的所有内容都是NULL.这是我正在使用的代码: 视图: @model ArticleCategoryVm@using (@Html.BeginForm()){ @Html.HiddenFor(model = model.LanguageIdDisplayed) labelName: span class="label lab
我有一个问题,当我发布到控制器,我失去绑定,我的视图模型中的所有内容都是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源代码,以便我可以调试它,看看发生了什么.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读