组HTML数据注解属性 [英] Set html attributes with data annotation

查看:59
本文介绍了组HTML数据注解属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置属性时,我的模型属性与任何HTML帮助渲染?
例:
我有我的自定义数据注解:

  [MyCustomAttribute(AnyHtmlProperty =100)]
公共字符串名称{;设置;}

所以,当这个属性被渲染,我想是这样的:

 <输入类型=文本anyHtmlProperty =100/>


解决方案

您可以写它接受一个前pression一个的HtmlHelper扩展方法。这里的剃刀语法会是什么样子无功能的例子。

  @ Html.CustomInputFor(X => x.Name)

How can I set attributes when my model property is rendered with any Html helper? Example: I have my custom data annotation:

[MyCustomAttribute(AnyHtmlProperty = "100")]
public string Name{get;set;}

So, when this property is rendered, I want something like this:

<input type="text" anyHtmlProperty="100" />

解决方案

You could write an HtmlHelper extension method which accepts an expression. Here's a non-functioning example of what the razor syntax would look like.

@Html.CustomInputFor(x => x.Name)

这篇关于组HTML数据注解属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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