我正在尝试使用 Microsoft 提供的非侵入式验证来处理 ASP.NET MVC 3 项目中的 errorPlacement JQuery Validate 插件。我永远无法点击 errorPlacement 功能,我不确定我做错了什么。我在下面提供模型/视图/控制器的代码。请让我知道我做错了什么?
看法
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<Project.Models.SampleModel>" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title>Index</title>
</head>
<body>
<script src="<%: Url.Content("~/Scripts/jquery-1.4.4.min.js") %>" type="text/javascript"></script>
<script src="<%: Url.Content("~/Scripts/jquery.validate.js") %>" type="text/javascript"></script>
<script src="<%: Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js") %>" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function ()
{
$("#form").validate(
{
errorPlacement: function (error, element)
{
alert("Code to define customer error");
}
})
});
</script>
<% using (Html.BeginForm("Index", "Sample", FormMethod.Post, new { id = "form" }))
{ %>
<%: Html.ValidationSummary(false) %>
<fieldset>
<legend>NewModel</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.Name) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.Name) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Age) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.Age) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Address) %>
</div>
<div class="editor-field">
<%: Html.EditorFor(model => model.Address) %>
</div>
<br />
<p>
<input type="submit" id="submit" value="Submit" />
</p>
</fieldset>
<% } %>
</body>
</html>
模型
public class SampleModel
{
[Required]
[DataType(DataType.Text)]
[DisplayName("Name")]
public string Name { get; set; }
[Required]
[DataType(DataType.Text)]
[DisplayName("Age")]
public string Age { get; set; }
[Required]
[DataType(DataType.MultilineText)]
[DisplayName("Address")]
public string Address { get; set; }
}
控制器
public class SampleController : Controller
{
//
// GET: /New/
public ActionResult Index()
{
return View();
}
}