http://haacked.com/archive/2009/11/19/aspnetmvc2-custom-validation.aspx
仍然无法使用jQuery验证来使用MVC2。我可以得到非jQuery工作,但当我换掉JS文件时它不起作用。它发生在第3天,我完全陷入困境。所以这就是我所拥有的。感谢您的帮助。
的Site.Master
<script src="<%= this.ResolveClientUrl("~/Resources/js/jquery-1.4.1.js") %>"
type="text/javascript"></script>
<script src="<%= this.ResolveClientUrl("~/Resources/js/jquery.validate.js") %>"
type="text/javascript"></script>
<script src="<%= this.ResolveClientUrl("~/Resources/js/MicrosoftMvcJqueryValidation.js") %>"
type="text/javascript"></script>
<script src="<%= this.ResolveClientUrl("~/Resources/js/ourJS--VERSION--.js") %>" type="text/javascript"></script>
<script src="<%= this.ResolveClientUrl("~/Resources/js/json2.js") %>" type="text/javascript"></script>
<link href="../../Resources/css/ourCSS--VERSION--.css" rel="stylesheet" type="text/css" />
视图模型:
namespace OurNamespace
{
[ExcludeFromCodeCoverage]
public class OurDataModelView : PersistedDataModelView
{
public OurModelView () : base()
{
ID = -1;
StartDate = DateTime.MinValue;
EndDate = DateTime.MinValue;
Description = string.Empty;
Deleted = false;
}
[DisplayFormat(DataFormatString = "{0:MM/yyyy}")]
public DateTime? StartDate { get; set; }
[DisplayFormat(DataFormatString = "{0:MM/yyyy}")]
public DateTime? EndDate { get; set; }
[Required(ErrorMessage="Description is required.")]
[StringLength(250000, ErrorMessage = "A maximum of 250000 characters are allowed")]
public string Description { get; set; }
public int? ID { get; set; }
public bool Deleted { get; set; }
}
}
ASPX页面
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
...
<% Html.RenderAction(WebConstants.ACTION_DISPLAY_HEADER, WebConstants.CONTROLLER, new { id = ViewData["ID"] }); %>
...
我们部分认为验证是在:
完成的 <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<OurNameSpace.OurDataModelView>" %>
...
<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm("", "", FormMethod.Post , new { id = "HeaderForm", onSubmit="return false;"})) { %>
<%: Html.ValidationSummary(false, "validation failed") %>
<%: Html.HiddenFor(model => model.ID) %>
<div class="form-row">
<div class="form-label">Description</div>
<div class="form-element"><%: Html.TextAreaFor(model => model.Description)%></div>
<div><%= Html.ValidationMessageFor(model => model.Description) %></div>
</div>
<div class="buttons">
<input id="Save" type="submit" class="save-button" value="" />
<div id="Cancel" class="cancel-button"></div>
</div>
<% } %>
...
* * SO在这里不起作用就是我所看到的。 在观察源我看到:
//<![CDATA[
if (!window.mvcClientValidationMetadata) { window.mvcClientValidationMetadata = []; }
window.mvcClientValidationMetadata.push({"Fields":[{"FieldName":"Description","ReplaceValidationMessageContents":true,"ValidationMessageId":"Description_validationMessage","ValidationRules":[{"ErrorMessage":"A maximum of 250000 characters are allowed","ValidationParameters":{"minimumLength":0,"maximumLength":250000},"ValidationType":"stringLength"},{"ErrorMessage":"Description is required.","ValidationParameters":{},"ValidationType":"required"}]}],"FormId":"HeaderForm","ReplaceValidationSummary":true,"ValidationSummaryId":"validationSummary"});
//]]>
但是,客户端验证没有出现。我在一个字符中单击textarea类型并从元素中删除它unocus,没有任何反应。有没有调试过的想法?
答案 0 :(得分:1)
这是一个非常简单的工作示例:
型号:
public class MyViewModel
{
[Required(ErrorMessage = "Description is required.")]
[StringLength(10, ErrorMessage = "A maximum of 10 characters are allowed")]
public string Description { get; set; }
}
控制器:
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new MyViewModel());
}
}
查看:
<script type="text/javascript" src="<%= Url.Content("~/scripts/jquery-1.4.1.js") %>"></script>
<script type="text/javascript" src="<%= Url.Content("~/scripts/jquery.validate.js") %>"></script>
<script type="text/javascript" src="<%= Url.Content("~/scripts/MicrosoftMvcJQueryValidation.js") %>"></script>
<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm()) { %>
<%: Html.TextAreaFor(x => x.Description)%></div>
<%: Html.ValidationMessageFor(x => x.Description) %>
<input type="submit" value="OK" />
<% } %>
MicrosoftMvcJQueryValidation.js
未包含在标准项目模板中,因为它是MVC Futures project的一部分(从源代码中提取)。
一旦你将这个例子投入使用并证明它可行,你就可以开始添加功能了。