ヘルパー:
public static MvcHtmlString MyValidationSummary(this HtmlHelper helper, bool excludePropertyErrors = false)
{
string html = "";
html += helper.ViewData.ModelState.IsValid ? "<div class='form-errors' style='display:none;'>" : "<div class='form-errors'>";
html += "<p>Please have another look at the fields highlighted below</p>";
html += helper.ValidationSummary(excludePropertyErrors);
html += "</div>";
return new MvcHtmlString(html);
}
意見:
@using (Html.BeginForm("Index", "Home",FormMethod.Post, new {id="myform"}))
{
@Html.MyValidationSummary()
...
<input type="button" id="submitbutton" value="Submit" />
}
JS:
$("#submitbutton").click(function () {
$("#myform").validate();
if (!$("#myform").valid()) {
$("#myform .form-errors").show();
}
else {
$("#myform").submit();
}
});
アップデート:
部分ビューで行きたい場合
Shared / _MyValidationSummary.cshtml
@if (!ViewData.ModelState.IsValid)
{
<div class="form-errors">
<p>Please have another look at the fields highlighted below</p>
@Html.ValidationSummary()
</div>
}
else
{
<div class="form-errors" style="display:none;">
<p>Please have another look at the fields highlighted below</p>
@Html.ValidationSummary()
</div>
}
意見:
@using (Html.BeginForm("Index", "Home",FormMethod.Post, new {id="myform"}))
{
@Html.Partial("_MyValidationSummary")
...
<input type="button" id="submitbutton" value="Submit" />
}