ASP.NET MVC 2的客户端验证规则不被创建 [英] ASP.NET MVC 2 client-side validation rules not being created
问题描述
MVC不生成视图模型我在客户端验证规则。该HTML只包含这样的:
MVC isn't generating the client-side validation rules for my viewmodel. The HTML just contains this:
<script type="text/javascript">
//<![CDATA[
if (!window.mvcClientValidationMetadata) { window.mvcClientValidationMetadata = []; }
window.mvcClientValidationMetadata.push({"Fields":[],"FormId":"form0","ReplaceValidationSummary":false});
//]]>
</script>
注意字段[]
是空的!
我的看法是强类型,并使用新的强类型HTML辅助( TextBoxFor()
等)。
My view is strongly-typed and uses the new strongly-typed HTML helpers (TextBoxFor()
, etc).
public class ItemFormViewModel
{
public Item Item { get; set; }
[Required] [StringLength(100)] public string Whatever { get; set; } // for demo
}
[MetadataType(typeof(ItemMetadata))]
public class Item
{
public string Name { get; set; }
public string SKU { get; set; }
public int QuantityRequired { get; set; }
// etc.
}
public class ItemMetadata
{
[Required] [StringLength(100)] public string Name { get; set; }
[Required] [StringLength(50)] public string SKU { get; set; }
[Range(0, Int32.MaxValue)] public int QuantityRequired { get; set; }
// etc.
}
(我知道我使用的是域模型的我的/我的的视图模型,这是不是一个好的做法的一部分,但无视现在。)
(I know I'm using a domain model as my / as part of my view model, which isn't a good practice, but disregard that for now.)
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
Inherits="System.Web.Mvc.ViewPage<ItemFormViewModel>" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Editing item: <%= Html.Encode(Model.Item.Name) %></h2>
<% Html.EnableClientValidation(); %>
<%= Html.ValidationSummary("Could not save the item.") %>
<% using (Html.BeginForm()) { %>
<%= Html.TextBoxFor(model => model.Item.Name) %>
<%= Html.TextBoxFor(model => model.Item.SKU) %>
<%= Html.TextBoxFor(model => model.Item.QuantityRequired) %>
<%= Html.HiddenFor(model => model.Item.ItemID) %>
<%= Html.TextBox("Whatever", Model.Whatever) %>
<input type="submit" value="Save" />
<% } %>
</asp:Content>
我包括在视图模型的属性,因为我怀疑MVC没有递归检查
ItemFormViewModel.Item <的子属性/ code>,但即使没有被验证?我甚至试过钻研MVC框架源$ C $ C,但都拿出了空。可能是什么回事?
I included the Whatever
property on the view model because I suspected that MVC wasn't recursively inspecting the sub-properties of ItemFormViewModel.Item
, but even that isn't being validated? I've even tried delving into the MVC framework source code but have come up empty. What could be going on?
推荐答案
大约5秒钟后我张贴的问题时,我意识到的东西:我的观点没有 ValidationMessage
占位符的任何地方。我加了&LT;%= Html.ValidationMessageFor(型号=&GT; model.Item.Name)%GT;
你瞧,MVC添加验证规则 Item.Name
来在页面底部的JS块。
About five seconds after I posted the question, I realized something: My view didn't have ValidationMessage
placeholders anywhere. I added <%= Html.ValidationMessageFor(model => model.Item.Name) %>
and lo and behold, MVC added validation rules for Item.Name
to the JS block at the bottom of the page.
原来,MVC不发出客户端验证规则字段的除非您实际执行下列操作之一:
It turns out that MVC does not emit client-side validation rules for a field unless you actually do one of the following:
- 呼叫
Html.ValidationMessage()
的属性。 - 呼叫
Html.Validate()
的财产。 (这一次将不会输出错误信息) - 使用渲染的控件
Html.EditorForModel()
。 (源)
- Call
Html.ValidationMessage()
for the property. - Call
Html.Validate()
for the property. (This one won't output error messages) - Render the controls using
Html.EditorForModel()
. (source)
做任何这些告诉MVC中,我的视图模型的这种特性是由用户可编辑的,所以你应该确认它。只使用HTML辅助贴在页面上的文本框 - 即使你使用新的强类型的帮手 - 是不够的。
Doing any of these tells MVC, "This property of my viewmodel is editable by the user, so you should be validating it." Just using the HTML helper to stick a textbox on the page -- even if you're using the new strongly-typed helpers -- isn't enough.
这篇关于ASP.NET MVC 2的客户端验证规则不被创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!