未创建 ASP.NET MVC 2 客户端验证规则

发布于 2024-08-25 16:30:06 字数 2228 浏览 6 评论 0原文

MVC 没有为我的视图模型生成客户端验证规则。 HTML 仅包含以下内容:

<script type="text/javascript">
//<![CDATA[
if (!window.mvcClientValidationMetadata) { window.mvcClientValidationMetadata = []; }
window.mvcClientValidationMetadata.push({"Fields":[],"FormId":"form0","ReplaceValidationSummary":false});
//]]>
</script>

请注意,Fields[] 为空!

我的视图是强类型的,并使用新的强类型 HTML 帮助器(TextBoxFor() 等)。

视图模型/域模型

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.
}

(我知道我正在使用域模型作为我的/作为我的视图模型的一部分,这不是一个好的实践,但现在忽略它。)

视图

<%@ 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>

我包含的 视图模型上的 Whatever 属性,因为我怀疑 MVC 没有递归检查 ItemFormViewModel.Item 的子属性,但即使这样也没有得到验证?我什至尝试深入研究 MVC 框架源代码,但一无所获。可能发生什么事?

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>

Note that Fields[] is empty!

My view is strongly-typed and uses the new strongly-typed HTML helpers (TextBoxFor(), etc).

View Model / Domain Model

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.)

View

<%@ 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>

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼眸里的快感 2024-09-01 16:30:06

发布问题后大约五秒钟,我意识到:我的视图在任何地方都没有 ValidationMessage 占位符。我添加了 <%= Html.ValidationMessageFor(model => model.Item.Name) %> ,你瞧,MVC 添加了 Item.Name 的验证规则到页面底部的JS块。

事实证明,MVC 不会为字段发出客户端验证规则除非您实际上执行了以下操作之一:

  1. 为属性调用 Html.ValidationMessage()
  2. 为该属性调用 Html.Validate()。 (这个不会输出错误消息)
  3. 使用 Html.EditorForModel() 呈现控件。 (source)

执行这些操作都会告诉 MVC,“我的这个属性viewmodel 可由用户编辑,因此您应该验证它。”仅使用 HTML 帮助程序在页面上粘贴文本框(即使您使用新的强类型帮助程序)也是不够的。

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.

It turns out that MVC does not emit client-side validation rules for a field unless you actually do one of the following:

  1. Call Html.ValidationMessage() for the property.
  2. Call Html.Validate() for the property. (This one won't output error messages)
  3. Render the controls using Html.EditorForModel(). (source)

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.

单身情人 2024-09-01 16:30:06

我没有运气让它在 MVC 2 RC 中工作。根据其他问题这里,你必须从 MVC Futures 版本中获取 MicrosoftMvcJQueryValidation.js 文件,将左脚放在脑后,并吹 Dixie 口哨半个小时。我做了这个以及更多,但未能使其发挥作用。

希望它能在 RTM 中得到修复。

I have had no luck getting this to work in MVC 2 RC. According to other questions here on SO, you have to get the MicrosoftMvcJQueryValidation.js file from the MVC Futures release, hold your left foot behind your head, and whistle Dixie for half an hour. I did this and more and have not been able to make it work.

Hopefully it will be fixed in RTM.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文