我的模型中的属性具有验证属性。如果我将模型传递给 View ,验证工作正常。但是,当该模型是 View 模型的属性并且 Controller 将 View 模型传递给 View 时,不显眼的验证将不再起作用,并且不会在 Controller 上进行验证。无论哪种方式,模型绑定(bind)到 Controller 都可以正常工作。
如果可以使用 View 模型中的对象的点表示法绑定(bind)隐藏字段等,为什么不引人注目的验证也不起作用,有人对如何使其起作用有建议吗?
View 模型(下面的第一个类是 View 使用的类,第一个属性上的“必需”与否没有什么区别)
public class PlanStakeholderViewModel : PlanGoalSelectorViewModel
{
[Required]
public PlanStakeholder PlanStakeholder { get; set; }
}
public class PlanGoalSelectorViewModel
{
public IList<PlanGoal> AvailableGoals { get; set; }
public IList<PlanGoal> SelectedGoals { get; set; }
public PostedGoals PostedGoals { get; set; }
}
public class PostedGoals
{
public string[] GoalIDs { get; set; }
}
查看
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
@Html.Hidden("PlanStakeholder.PlanID")
@Html.Hidden("PlanStakeholder.Id")
@Html.Partial("_Form")
<div class="editor-label"></div>
<div class="editor-field">
<input type="submit" value="Save" /> | @Html.ActionLink("Back to Plan", "Index", "Plan")
</div>
}
查看_表单
@model Navigator.Views.ViewModels.PlanStakeholderViewModel
<div class="editor-label" style="vertical-align:top">
@Html.LabelFor(model => model.PlanStakeholder.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.PlanStakeholder.Name)
@Html.ValidationMessageFor(model => model.PlanStakeholder.Name)
</div>
<div class="editor-label" style="vertical-align:top">
@Html.LabelFor(model => model.PlanStakeholder.Relationship)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.PlanStakeholder.Relationship)
@Html.ValidationMessageFor(model => model.PlanStakeholder.Relationship)
</div>
<div class="editor-label" style="vertical-align:top">
<label>Associated Goals</label>
</div>
<div class="editor-field checkbox">
@Html.CheckBoxListFor(x => x.PostedGoals.GoalIDs,
x => x.AvailableGoals,
x => x.Id,
x => x.ShortTitle,
x => x.SelectedGoals,
Position.Vertical)
</div>
型号
public class PlanStakeholder : Base
{
public PlanStakeholder()
{
PlanID = -1;
Relationship = String.Empty;
Name = String.Empty;
}
[Required]
public int PlanID { get; set; }
[Required]
[MaxLength(50, ErrorMessage = "The {0} cannot be more than {1} characters.")]
public string Relationship { get; set; }
[Required]
[MaxLength(50, ErrorMessage = "The {0} cannot be more than {1} characters.")]
public string Name { get; set; }
}
更新1 下面是关系属性的 html 表单的输出。
<input class="text-box single-line" data-val="true" data-val-required="The Relationship field is required." id="PlanStakeholder_Relationship" name="PlanStakeholder.Relationship" type="text" value="Mom" />
以下是 Controller 操作
[AuthorizeRoles(RoleSite.None, RoleOrg.OrgMember, RoleCohort.Client)]
public ActionResult Edit(int id, int planId)
{
var plan = PlanManager.GetSingle(CurrentUser.UserId, CurrentOrg.Id);
var model = new PlanStakeholderViewModel();
model.PlanStakeholder = PlanStakeholderManager.GetSingle(id, CurrentUser.UserId);
model.AvailableGoals = PlanGoalManager.GetList(plan.Id, CurrentUser.UserId);
model.SelectedGoals = PlanGoalManager.GetRelatedToStakeholder(id, CurrentUser.UserId);
if (model.Item.Id != id)
{
return HttpNotFound();
}
return View(model);
}
[HttpPost, ActionName("Edit")]
[AuthorizeRoles(RoleSite.None, RoleOrg.OrgMember, RoleCohort.Client)]
public ActionResult EditConfirmed(PlanStakeholderViewModel model)
{
if (ModelState.IsValid)
{
///DO STUFF
return RedirectToAction("Index", "Plan");
}
return View(model);
}
更新2 如果我在 View 中注释掉 CheckBoxListFor 则一切正常。看来我需要接受这个问题elsewhere 。未注释掉时,如果我输入的 PlanStakeholder.Name 值比模型允许的值长,我会在包含 @Html 的行上得到“值不能为空。参数名称:源”作为我的 asp.net 错误.CheckBoxListFor。
最佳答案
这是我倾向于不使用部分 View 的原因之一。他们有太多的陷阱,你必须了解各种细节。
我非常喜欢 EditorTemplates。它们正确处理集合并生成嵌套属性的正确名称。并且,他们处理 FormContext 问题。
但是……无论如何。将其添加到局部 View 的顶部。
@{ ViewContext.FormContext = new FormContext(); }
关于jquery - MVC 4 对 View 模型中的对象进行不显眼的验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13058900/