在MVC3应用程序中,我有一个视图呈现两个Ajax部分视图:创建局部视图和列表一个。在创建局部视图中,我尝试在OnBegin事件上触发客户端验证,但这不会发生。
当我强制错误时,我得到的错误消息是模型注释中指示的错误消息,而不是我在验证函数中定义的错误消息,称为OnBegin(例如:
将事件放置为空,我得到“需要事件的地方”(注释)而不是“它发生在哪里?”(FieldsValidation功能),
对于事件描述我得到“事件描述是必需的”(注释)而不是“告诉我们......关于它!”(FieldsValidation函数)
和验证摘要消息sais“创建时出错。请在更正后重试。”而不是“保存失败。请更正错误并重试。” (FieldsValidation函数))。
我试图在FieldsValidation函数中放置一个断点,但它从未到达过。 为了确保,我还尝试向OnBegin调用一个显示警报的函数(在测试之后我恢复了对FieldsValidation函数的原始调用):
function toDoOnBegin() { alert("This is onBegin"); }
警报永远不会显示!
如果所有字段都已正确填写,则事件将正确创建并显示在事件列表中(另一个部分视图)。问题是函数调用OnBegin唯一的。
请解释一下我做错了什么,我是否错过了包含某些内容或其他内容?
真的很感谢你。
的Web.config
<appSettings>
<add key="webpages:Version" value="1.0.0.0" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
型号:
public partial class Event
{
public int client_ID { get; set; }
[Required(ErrorMessage="The event date is required")]
[DataType(DataType.Date, ErrorMessage="The event date is invalid")]
public System.DateTime event_date { get; set; }
[Required(ErrorMessage="The event place is required")]
public string event_place{ get; set; }
[Required(ErrorMessage="The event description is required")]
public string event_description{ get; set; }
}
_Layout.cshtml:
样式表
<link href="@Url.Content("~/Content/styles.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/nivo-slider.css")" rel="stylesheet" type="text/css" media="screen"/>
<link href="@Url.Content("~/Content/themes/redmond/jquery-ui-1.8.18.custom.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/jquery.timepicker.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/jquery.alerts.css")" rel="stylesheet" type="text/css" media="screen"/>
jqueries for datetimepicker插件需要在其他几个页面上
<script src="@Url.Content("~/Scripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-ui-1.8.18.custom.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.ui.core.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.ui.slider.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.ui.datepicker.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.ui.datepicker-fr.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-ui-timepicker-addon.js")" type="text/javascript"></script>
其他应用程序组件的jQueries
<script src="@Url.Content("~/Scripts/jquery.alerts.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/OJscript.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.nivo.slider.pack.js")" type="text/javascript"></script>
jQueries进行验证
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
Index.cshtml
@model MyApp.ViewModels.EventViewModel
@{
ViewBag.Title = "Events";
}
<div id="divUpdateable">
@Html.Partial("_Create", Model)
</div>
<div id="divList">
@Html.Partial("_ListEvents", Model.EventsList)
</div>
_Create.cshtml
@model MyApp.ViewModels.EventViewModel
<script type="text/javascript">
function FieldsValidation() {
$("#idformCreate").validate({
errorContainer: $($('div.validation-summary-errors')),
errorLabelContainer: $("ul", $('div.validation-summary-errors')),
wrapper: 'li',
errorClass: "input-validation-error2",
rules:
{
'Event.event_date':
{
required: true
},
'Event.event_place':
{
required: true
},
'Event.event_description':
{
required: true
}
},
messages:
{
'Event.event_date':
{
required: "Date is required"
},
'Event.event_place':
{
required: "Where did it take place?"
},
'Event.event_description':
{
required: "Tell us sth about it!"
}
}
}).form();
if (!$("#idformCreate").valid()) {
$("div.validation-summary-errors span").html("Save failed. Please correct errors and retry.");
return false;
}
return true;
}
</script>
@using (Ajax.BeginForm("Create", "Event", new { clientID = Model.Event.client_ID }, new AjaxOptions
{
UpdateTargetId = "divUpdateable",
OnBegin = "FieldsValidation",
InsertionMode = InsertionMode.Replace,
OnSuccess = "getForm('" + Url.Action("ListEvents", "Event", null) + "','divList')"
}, new { id = "idformCreate" }))
{
@Html.ValidationSummary(false, "Error on creation. Please retry after corrections.")
<p>@Html.ValidationMessage("_FORM")</p>
<div class="validation-summary-errors">
<span></span>
<ul>
</ul>
</div>
<fieldset style="width: 800px; line-height: 1.4em;">
<legend>Add an event</legend>
<table style="width: 100%; padding-bottom: 0; padding-top: 0; border: 0;">
<tr>
<td style="width: 90px; border: 0;">
@Html.LabelFor(model => model.Event.event_date)
</td>
<td style="width: 200px; border: 0;">
@Html.EditorFor(model => model.Event.event_date, new { @class = "date" })
@Html.ValidationMessageFor(model => model.Event.event_date, "*")
@Html.HiddenFor(model => model.Event.client_ID)
</td>
</tr>
<tr>
<td style="width: 80px; border: 0; text-align: right;">
@Html.LabelFor(model => model.Event.event_place)
</td>
<td style="width: 120px; border: 0;">
@Html.DropDownList("Event.event_place", Model.PlacesList, "Where?", new { @style = "width: 100px;" })
@Html.ValidationMessageFor(model => model.Event.event_place, "")
</td>
</tr>
<tr>
<td style="width: 90px; border: 0;">
@Html.LabelFor(model => model.Event.event_description)
</td>
<td style="width: 600px; border: 0;" colspan="5">
@Html.TextAreaFor(model => model.Event.event_description, new { @class = "input_txt_nofloat", @style = "width:590px" })
@Html.ValidationMessageFor(model => model.Event.event_description, "*")
</td>
</tr>
<tr style="height: 64px;">
<td colspan="2" style="text-align: center;">
<br />
<br />
<input type="submit" value="Save" class="submit" style="width: 280px" />
</td>
</tr>
</table>
</fieldset>
}
答案 0 :(得分:3)
Here is the answer:(非常感谢BrockAllen)
如果您正在进行自己的客户端验证,那么您应该这样做 删除jquery.validate.unobtrusive.min.js。这是MVC的客户端 验证码,如果两者都在使用,它们将发生冲突 jquery.validate。
希望这对其他人也有帮助。
答案 1 :(得分:0)
开始时你正在打电话:
OnBegin = "FieldsValidation",
如果您希望显示警报,则需要:
OnBegin = "toDoOnBegin"