Ich habe ein Problem, dass, wenn ich auf einen Controller posten ich Bindung verlieren und alles in meinem View-Modell ist NULL. Hier ist der Code ich verwende:ASP.NET MVC View Model nicht verbindlich auf HTTP Post mit DropDownList
Ausblick:
@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>
}
Ansichtsmodell:
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)
}
Warum ist alles in meinem ViewModel NULL? Ich kann bei der Verwendung Chromes Tools sehen, dass die Werte in der Post werden für die Saiten und Ints ... Als ein Werk geschrieben werden um ich gerade getan haben, die nach dem Code zum Laufen zu bringen:
Abhilfe 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)
}
Dank
Das trieb mich NUTS. Manchmal siehst du nicht, was direkt vor deinen Augen ist. –
Dieser Fix hat perfekt funktioniert. Es hat mich auch verrückt gemacht, da ich die Daten während der Überwachung der Anfrage klar gesehen habe. –