gpt4 book ai didi

asp.net-mvc-3 - ASP.NET MVC View 模型未绑定(bind)到带有 DropDownList 的 HTTP Post

转载 作者:行者123 更新时间:2023-12-03 14:26:47 24 4
gpt4 key购买 nike

我有一个问题,当我发布到 Controller 时,我失去了绑定(bind),并且我的 View 模型中的所有内容都是 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, "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; }
}

Controller :
[HttpPost]
public ActionResult Categories(ArticleCategoryVm vm)
{
// Code here
if (ModelState.IsValid)
{
}

ReDrawDropDowns(vm);
return View(vm)
}

为什么我的 View 模型中的所有内容都为 NULL?我可以在使用 Chromes 工具时看到,在帖子中为字符串和整数发布了值...作为解决方法,我刚刚完成了以下操作以使代码正常工作:

解决方法 Controller :
[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)
}

谢谢

最佳答案

要在 View 中建模绑定(bind),您应该使用 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" })

关于asp.net-mvc-3 - ASP.NET MVC View 模型未绑定(bind)到带有 DropDownList 的 HTTP Post,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15227815/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com