這工作:@Html.ValidationMessage("Name")
ViewModel中是否有強類型驗證消息?
問題是否有可能獲得強類型的輔助工作?:@Html.ValidationMessageFor(model => model.EventInVM.Name)
視圖模型:
public class EventViewModel
{
public Event EventInVM { get; set; }
public IList<Series> ListOfSeries { get; set; }
}
控制器:
[HttpPost]
public ActionResult Create(EventViewModel eventViewModel)
{
if (!ModelState.IsValid)
{
SetupDropDownsStronglyTyped(eventViewModel);
return View(eventViewModel);
}
uow.Add(eventViewModel.EventInVM);
uow.SaveChanges();
return RedirectToAction("Index");
}
查看
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(false)
<fieldset>
<legend>Event</legend>
@Html.Partial("_CreateOrEdit", Model)
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
部分
@model dave.Controllers.EventViewModel
<div class="editor-label">
@Html.LabelFor(model => model.EventInVM.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EventInVM.Name)
@Html.ValidationMessageFor(model => model.EventInVM.Name)
</div>
編輯模板,您使用的數據標註爲您驗證?例如,你的'Event'類型'Name'屬性是否有'[Required]'屬性或類似的東西? – ataddeini 2011-04-26 02:45:57
不,我不是。我使用LightSpeed作爲我的ORM和一個自定義EntityModelBinder來流過註釋。我認爲這是工作正常,因爲驗證摘要顯示我期望..已經上傳截圖 – 2011-04-26 02:55:27
行。我猜錯誤沒有正確的鍵被添加到'ModelState.Errors'集合。如果錯誤沒有與屬性名稱匹配的鍵,那麼'Html.ValdiationMessageFor'不會顯示它,但摘要仍然會顯示。 – ataddeini 2011-04-26 03:21:14