도우미 :
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();
}
});
업데이트 :
이 부분으로 이동하려면 보기
공유/_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" />
}
아주 좋은 방법, sormii입니다. 나는 html에 대한 실제 싫어함이 그런 문자열에 묻혀 있기 때문에 HTML 도우미를 부분 뷰로 전환하는 것은 쉽지 않다고 생각한다. – SimonF
어쨌든 부분보기 접근법에 대한 답변이 업데이트되었습니다. –
좋은 하나! 누군가가 더 우아한 해결책을 찾을 때까지 나는 진드기를 줄 것이다.) – SimonF