我遇到的问题是,当我发布到控制器时,我会丢失绑定,并且我的视图模型中的所有内容都为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; }
}
控制器:
[HttpPost]
public ActionResult Categories(ArticleCategoryVm vm)
{
// Code here
if (ModelState.IsValid)
{
}
ReDrawDropDowns(vm);
return View(vm)
}
为什么我的viewmodel中的所有内容都为NULL?我可以看到使用Chromes工具,在帖子中为字符串和整数发布值...作为一个解决方法我刚刚完成以下工作以使代码工作:
解决方法控制器:
[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)
}
由于
答案 0 :(得分:40)
看起来可能是因为this question中提到的属性和变量之间存在差异。将{ get; set; }
添加到要绑定的所有变量。正如该问题所述,我认为这是因为默认模型绑定器使用反射的方式。
我发现在过去实际获取ASP.NET MVC源代码是有用的,这样我就可以调试它并看看发生了什么。
答案 1 :(得分:37)
视图模型为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和MVC4。
答案 2 :(得分:2)
要在View中建模绑定,您应该使用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" })