Failed to create ASP.NET MVC 2 Client Validation Rules

MVC does not create client-side validation rules for my viewmodel. HTML only contains this:

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

Note that Fields[] empty!

My view is strongly typed and uses 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 that I am using the domain model as my / part of my presentation model, which is not good practice, but ignore it 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 in the view model because I suspected that MVC does not recursively check the sub-properties of ItemFormViewModel.Item , but even this is not checked? I even tried to delve into the source code of the MVC environment, but came up with an empty one. What could be?

+3
source share
2 answers

About five seconds after I posted the question, I realized something: my view did not have ValidationMessage placeholders anywhere. I added <%= Html.ValidationMessageFor(model => model.Item.Name) %> and now, 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 the field , unless it actually does one of the following:

  • Call Html.ValidationMessage() for the property.
  • Call Html.Validate() for the property. (This will not display error messages)
  • Mark the controls with Html.EditorForModel() . ( source )

Executing any of them tells MVC: "This property of my view model is editable by the user, so you should check it." Just using an HTML helper to insert a text box on a page - even if you are using new strongly typed helpers - is not enough.

+9
source

I was not lucky this worked in MVC 2 RC. For other questions here on SO, you should get the MicrosoftMvcJQueryValidation.js file from the MVC Futures release, keep your left foot behind your head and whistle Dixie in half an hour. I did this and more and could not get it to work.

Hope it will be fixed in RTM.

-1
source

Source: https://habr.com/ru/post/1304660/


All Articles