我的模型中的属性有验证属性。如果我将模型传递给视图,验证工作正常。但是,当该模型是视图模型的属性并且控制器将视图模型传递给视图时,不显眼的验证将不再起作用,并且不会在控制器上进行验证。绑定到控制器的模型在任何一种情况下都可以正常工作。
如果可以绑定隐藏字段,并且对视图模型中的对象使用点表示法,为什么不显眼的验证也有效,是否有人就如何使其工作有建议?
视图模型(下面的第一个类是视图使用的,“必需”或不在它的第一个属性上没有区别)
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>
}
查看_Form
@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>
MODEL
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 下面是Relationship属性的表单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" />
以下是控制器操作
[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 如果我在视图中注释掉CheckBoxListFor,那么一切正常。看起来我需要提出这个问题/问题elsewhere。如果没有注释掉,如果我为PlanStakeholder.Name输入一个比模型允许的更长的值,我会得到“值不能为空。参数名称:source”作为我在拥有@Html的行上的asp.net错误.CheckBoxListFor。
答案 0 :(得分:0)
这是我倾向于不使用部分视图的一个原因。他们有太多陷阱,你必须知道各种细节。
我非常喜欢EditorTemplates。它们正确处理集合并生成嵌套属性的正确名称。并且,它们处理FormContext问题。
但是......无论如何。将其添加到局部视图的顶部。
@{ ViewContext.FormContext = new FormContext(); }