如何读取.NET MVC特性数据标注值 [英] How to read property data annotation value in .NET MVC

查看:133
本文介绍了如何读取.NET MVC特性数据标注值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始瓦特/ ASP.NET MVC 3,我试图呈现出在创建/编辑视图在一个视图模型的字符串属性下面的HTML。

I Just starting out w/ ASP.NET MVC 3 and I am trying to render out the following HTML for the string properties on a ViewModel on the create/edit view.

<input id="PatientID" name="PatientID" placeholder="Patient ID" type="text" value="" maxlength="30" />

每个值的关系回的视图模型,ID和放大器的财产;名称是属性名称,占位符是显示属性值属性的值,最大长度为StringLength属性。

Each value ties back to the property on the ViewModel, id & name are the property name, placeholder is the Display attribute, value is the value of the property, and maxlength is the StringLength attribute.

相反瓦特/我的每个字符串属性的正确值输入了上面的HTML我想我会尝试SingleLineTextBox的名称创建一个EditorTemplate和我的字符串属性使用UIHint或者通过视图时的名称我叫EditFor。到目前为止好,但我无法弄清楚如何获得最大长度值关StringLength属性。

Instead of typing out the above HTML w/ the correct values for each of my string properties I thought I would try to create an EditorTemplate by the name of SingleLineTextBox and use UIHint on my string properties or pass the name of the view when I call EditFor. So far so good, except I can't figure out how to get the maxlength value off the StringLength attribute.

下面是code我到目前为止有:

Here is the code I have so far:

<input id="@ViewData.ModelMetadata.PropertyName" name="@ViewData.ModelMetadata.PropertyName" placeholder="@ViewData.ModelMetadata.DisplayName" type="text" value="@ViewData.Model" maxlength="??" />

正如你所看到的,不知道如何设置最大长度值。任何人都知道怎么样?

As you can see, not sure how to set maxlength value. Anyone know how?

另外,我要对这个的最好方法?正如我以前说过我可以只写出来的纯HTML我自己的页面上的每个属性。我看着使用TextBoxFor它没有设置最大长度,并加入一些验证标记的HTML输出,因为我不想让StringLength属性。我看到了另一种选择是关闭HTML类扩展/帮手。

Also, am I going about this the best way? As I said before I could just write out the plain HTML myself for each property on the page. I've looked at using TextBoxFor it wasn't setting the maxlength and was adding a bunch of validation markup to the HTML output because of the StringLength attribute which I do not want. Another option I saw was extensions/helpers off the HTML class.

推荐答案

而不是 StringLength 属性的(因为它是一个验证器属性没有元数据提供者)可以使用 AdditionalMetadata 属性。示例用法:

Instead of the StringLength attribute (because it's a validator attribute not a metadata provider) you can use the AdditionalMetadata attribute. Sample usage:

public class ViewModel
{
    [AdditionalMetadata("maxLength", 30)]
    public string Property { get; set; }
}

基本上,它把值30在 ViewData.ModelMetadata.AdditionalValues​​ 词典中的键最大长度下。所以,你可以使用它你EditorTemplate:

Basically it puts the value 30 under the key maxLength in the ViewData.ModelMetadata.AdditionalValues dictionary. So you can use it your EditorTemplate:

<input maxlength="@ViewData.ModelMetadata.AdditionalValues["maxLength"]" id="@ViewData.ModelMetadata.PropertyName" name="@ViewData.ModelMetadata.PropertyName" placeholder="@ViewData.ModelMetadata.DisplayName" type="text" value="@ViewData.Model"  />

这篇关于如何读取.NET MVC特性数据标注值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆