我有一個DropDownList控件和一個文本框的局部視圖。在asp.net中驗證錯誤是不可見partialView mvc
當什麼也沒有選擇或輸入任何文字ModelState.IsValid是FALSE
但我可以看到任何驗證錯誤顯示的註釋上我的財產。
這是爲什麼?例如,我可以在ModelState.Value.Errors屬性中看到「缺少名稱」
「。
當我確認我的輸出窗口中的無效視圖我看到這一點:
A first chance exception of type 'System.InvalidOperationException' occurred in System.Web.Mvc.dll
A first chance exception of type 'System.InvalidOperationException' occurred in System.Web.Mvc.dll
A first chance exception of type 'System.InvalidOperationException' occurred in System.Web.Mvc.dll
public ActionResult Edit()
{
return LoadEditTemplates();
}
[HttpPost]
public ActionResult Update(EditTemplateListViewModel viewModel)
{
if (ModelState.IsValid && !_templateDataProvider.TemplateExists(viewModel.Name))
{
Template template = Mapper.Map<EditTemplateListViewModel, Template>(viewModel);
_templateDataProvider.UpdateTemplate(template);
return new JsonNetResult(new { success = true });
}
return PartialView(viewModel);
}
private ActionResult LoadEditTemplates()
{
var templates = _templateDataProvider.GetTemplates();
EditTemplateListViewModel editTemplateViewModel = new EditTemplateListViewModel()
{
DisplayList = Mapper.Map<IEnumerable<Template>, IEnumerable<TemplateViewModel>>(templates),
};
return PartialView(editTemplateViewModel);
}
public class EditTemplateListViewModel
{
[Required(ErrorMessage = "No template selected.")]
public int TemplateId { get; set; }
[Required(ErrorMessage="Name is missing")]
public string Name { get; set; }
public IEnumerable<TemplateViewModel> DisplayList { get; set; }
}
@model ITMS.Web.Models.EditTemplateListViewModel
@*Remote Validation*@
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm("Update", "Template"))
{
@Html.ValidationSummary(false)
@Html.DropDownListFor(x => x.TemplateId, new SelectList(Model.DisplayList, "TemplateId", "Name"), new { @class = "listviewmodel" })
<p class="editor-label">@Html.LabelFor(model => model.Name)</p>
<p class="editor-field">@Html.EditorFor(model => model.Name)</p>
<p class="editor-field">@Html.ValidationMessageFor(model => model.Name)</p>
}
沒有幫助。恰好相反。當我用部分視圖打開jquery對話框時,在輸出窗口中會看到很多異常。 – Elisabeth
如果部分視圖異步加載,則驗證插件需要重新初始化。檢查http://stackoverflow.com/questions/7005052/trouble-getting-unobtrusive-validation-working-with-mvc-3-on-jquery-ajax-post和http://stackoverflow.com/questions/12516245/how -to-check-if-a-div-is-valid-with-unobtrusive-javascript – Sunny
我所有的部分視圖都是異步加載的。除了jquery.validate.unobtrusive.min.js之外,我從不需要任何更進一步的jquery庫。「在我這邊沒有動態內容。 – Elisabeth