开发者

Template with Required Attribute Knowledge

开发者 https://www.devze.com 2023-02-10 09:19 出处:网络
This seems very basic but I\'m struggling to find a solution I\'m happy with at the minute. All I want to do is add a class with the name \"Required\" to any fields that have a required property again

This seems very basic but I'm struggling to find a solution I'm happy with at the minute. All I want to do is add a class with the name "Required" to any fields that have a required property against them so:

public class Dummy{
  [Required]
  public string Name {get; set;}
}

Would be called with

@Html.EditorFor(model=>model.Name)

and would output something like

<input id="Name *Data-VAL and other attribs* class="Required" />

With all t开发者_运维问答he inbuilt unobtrusive goodness etc. I'm using Razor and MVC 3. Any help much appretiated


You may take a look at the following blog post which explains how you could write a custom DataAnnotationsModelMetadataProvider to achieve this.


using System.ComponentModel.DataAnnotations; //You need to import this namespace

public class Dummy {
   [Required]
   public string Name { get; set; }
}

Further reference: http://www.asp.net/mvc/tutorials/validation-with-the-data-annotation-validators-cs

0

精彩评论

暂无评论...
验证码 换一张
取 消