客户端验证在 Ajax.BeginForm 中对我不起作用
这是我的代码:
<div id="report">
<div id="projectReport">
<div >
@{
Html.EnableClientValidation();
}
@using (Ajax.BeginForm("AnalyticsDates", new AjaxOptions
{
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "reportContent"
}))
{
@Html.LabelFor(m => m.StartDate)
@Html.TextBoxFor(m => m.StartDate, new { id = "start" })
@Html.ValidationMessageFor(model => model.StartDate)
@Html.LabelFor(m => m.EndDate)
@Html.TextBoxFor(m => m.EndDate, new { id = "end" })
@Html.ValidationMessageFor(model => model.EndDate)
<input id="btnsearch" type="submit" value=@Titles.Search class="iconHeader"/>
}
</div>
</div>
<div id="reportContent">
</div>
</div>
我在 web.config 页面中启用了验证:
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
并添加了js文件
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
第二个问题与第一个问题有关,我的行动是
[HttpPost]
[Authorize(Roles = "XXXReport")]
public async Task<ActionResult> AnalyticsDates(ReportRequestVM reportRequestVM)
{
if (!ModelState.IsValid)
{
return View("**MainReports**", reportRequestVM);
}
// fill reportRequestVM with data
return View("**PartialReport**", reportRequestVM);
}
如果模型是有效的,我返回一个局部视图并且页面看起来不错,否则我返回主视图,带有表单,但是在这个页面中,它会自我呈现两次。问题是,如果客户端验证失败,如何返回带有验证错误的主表单?
任何帮助将不胜感激,10x Rony