幫手:
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