Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc MVC4如何使用模型添加htmlattribute类_Asp.net Mvc - Fatal编程技术网

Asp.net mvc MVC4如何使用模型添加htmlattribute类

Asp.net mvc MVC4如何使用模型添加htmlattribute类,asp.net-mvc,Asp.net Mvc,我只是想通过我的模型类添加一个类名 public partial class tblLocation { /*how to add html attribute class=myclass*/ public global::System.String SitePlan { get; set; } } 在我的查看页面中 @Html.EditorFor(model => model.SitePlan) 最后,我需要这种输出 <input class="myclass" data-v

我只是想通过我的模型类添加一个类名

public partial class tblLocation { 
/*how to add html attribute class=myclass*/
public global::System.String SitePlan { get; set; }
}
在我的查看页面中

@Html.EditorFor(model => model.SitePlan)
最后,我需要这种输出

<input class="myclass" data-val="true" data-val-required="*" id="SitePlan" name="SitePlan" type="text">

您可以指定模型属性名称,如下所示

@Html.EditorFor(model => model.SitePlan, new { @class = Model.myclass })
换句话说:class=@Model.myclass

例:


您可以创建自己的属性来实现IMetadataAware和自定义html帮助程序,或者将属性添加到model@html.TextBoxForm=>m.SitePlan,新的{@class=model.YourClassNameProperty}解决方案中。但是先生,我不想在我的浏览页面上写任何东西。我希望新的{@class=Model.myclass}通过模型类自动到达新的{@class=Model.myclass}-你不想这样写,或者完全不想在视图中写任何东西?如果不在视图中写入任何内容,Css类将不会从模型中分配。
<input class="@Model.myclass" data-val="true" data-val-required="*" id="SitePlan" name="SitePlan" type="text">