通过HTML属性和[DisplayFormat]使用Html.EditorFor和Html.TextBox [英] Passing HTML Attributes and [DisplayFormat] using Html.EditorFor and Html.TextBox

查看:290
本文介绍了通过HTML属性和[DisplayFormat]使用Html.EditorFor和Html.TextBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Html.EditorFor 对,我既需要 DisplayFormat 属性和HTML值要应用属性(specfically CSS类)。

I am trying to use Html.EditorFor for a value where I need both a DisplayFormat attribute and an HTML attribute (specfically a CSS class) to be applied.

Html.TextBox 忽略 DisplayFormat 属性和 Html.EditorFor 不会让我过去的HTML属性。除了编写HTML自己,什么是这里的preferred解决方案?

Html.TextBox ignores the DisplayFormat attribute, and Html.EditorFor will not let me pass Html attributes. Other than writing the HTML myself, what is the preferred solution here?

推荐答案

这里有一个可能性:

public class MyModel
{
    [UIHint("MyDateTemplate")]
    public DateTime Value { get; set; }
}

〜/查看/主页/ EditorTemplates / MyDateTemplate.ascx

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DateTime>" %>
<%: Html.TextBox(
    string.Empty, 
    Model.ToString("dd/MM/yyyy"), 
    new { @class = "foo" }
)%>

和在主视图:

<%: Html.EditorForModel()

或另一个之一:

public class MyModel
{
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime Value { get; set; }
}

和在你的主视图:

<div class="foo">
    <%: Html.EditorFor(x => x.Value) %>
</div>

和在你的CSS:

.foo input {
    /* define some rule here */
}

这篇关于通过HTML属性和[DisplayFormat]使用Html.EditorFor和Html.TextBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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