我的模型中的属性有验证属性。如果我将模型传递给视图,则验证工作正常。但是,当该模型是 View Model 的属性并且控制器将 View Model 传递给视图时,不显眼的验证不再起作用,并且不会在控制器上得到验证。无论哪种方式,绑定到控制器的模型都可以正常工作。
如果可以在视图模型中使用点符号为对象绑定隐藏字段等,为什么不显眼的验证也不起作用,是否有人对如何使其工作有建议?
视图模型(下面的第一个类是视图使用的,“必需”与否在其第一个属性上没有区别)
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" />
下面是控制器动作
[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 ,那么一切正常。看来我需要在别处提出这个问题/问题。当没有注释掉时,如果我输入的 PlanStakeholder.Name 值比模型允许的值长,我会得到“值不能为空。参数名称:源”作为我的 asp.net 错误在具有@Html 的行上.CheckBoxListFor。