简体   繁体   English

Html.TextBoxFor格式化还是Html.EditorFor htmlAttributes?

[英]Html.TextBoxFor formatting or Html.EditorFor htmlAttributes?

I am kind of stumped because, I want to format the value and add a html attribute for css class. 我有点难过,因为我想格式化值并为css类添加一个html属性。

If I use @Html.TextBoxFor(m => m.DateModified) - I can add html attribute but formatting does not work via DisplayFormat attribute on the member. 如果我使用@Html.TextBoxFor(m => m.DateModified) - 我可以添加html属性,但格式不能通过成员上的DisplayFormat属性工作。

If I use @Html.EditorFor(m => m.DateModified) - Formatting works but I cannot add html attribute 如果我使用@Html.EditorFor(m => m.DateModified) - 格式化工作,但我无法添加html属性

If I use @Html.TextBox("DateModified", Model.DateModified, ...) - I get null reference exception when Model is null when the form is in add mode 如果我使用@Html.TextBox("DateModified", Model.DateModified, ...) - 当表单处于添加模式时,当Model为null时,我得到空引用异常

What is the best way to achieve this? 实现这一目标的最佳方法是什么?

I ended up solving this by creating a custom editor template for my date picker as so: 我最终通过为我的日期选择器创建自定义编辑器模板来解决这个问题:

Shared/EditorTemplates/DateTime.cshtml 共享/ EditorTemplates / DateTime.cshtml

 @model System.DateTime? 
 @Html.TextBox("", Model.HasValue ? Model.Value.ToString("dd/MM/yyyy") : string.Empty, new { @class = "date-picker" })

Then in my original page continue to use 然后在我的原始页面继续使用

@Html.EditorFor(m => m.DateModified)

你可以...

@Html.TextBoxFor(m => m.DateModified, new { Value = Model.DateModified.ToString("MM-dd-yyyy"), @class = "superCoolClassName"})

Use @Html.EditorFor(m => m.DateModified), because otherwise the DisplayFormat attribute will have no effect. 使用@ Html.EditorFor(m => m.DateModified),否则DisplayFormat属性将无效。

To add further attributes like a CSS class, you have to create an editor template for the DateTime. 要添加CSS类等其他属性,必须为DateTime创建编辑器模板。 Create a file EditorTemplates/DateTime.cshtml with the following content: 使用以下内容创建文件EditorTemplates / DateTime.cshtml:

@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new
{
    @class="date"
})

Please note that the value of the TextBox is not set with the Model directly, but rather with the TemplateInfo.FormattedModelValue , because that value will be formatted according to the DisplayFormat attribute while the Model not. 请注意,TextBox的值不是直接使用Model设置的,而是使用TemplateInfo.FormattedModelValue设置的 ,因为该值将根据DisplayFormat属性进行格式化,而Model不会。 (This took me quite some time to realize. :)) (这花了我很长时间才意识到。:))

In simple cases this might be enough, eg if the CSS class can be the same for all date editors. 在简单的情况下,这可能就足够了,例如,如果所有日期编辑器的CSS类都相同。

If you want to parametrize the attribute, you can do that as well, passing the attribute value parameter to the EditorFor. 如果要对属性进行参数化,也可以将属性值参数传递给EditorFor。

@Html.EditorFor(m => m.DateModified, new { @class = "someClass" })

However, this parameter will be not automagically delegated to the HTML control as attribute, but you have to "handle it" in the template explicitly. 但是,此参数不会作为属性自动委派给HTML控件,但您必须明确地在模板中“处理”它。 According to my experiences you can access this parameter value in the ViewData in the template, so the parametrized template looks like this: 根据我的经验,您可以在模板中的ViewData中访问此参数值,因此参数化模板如下所示:

@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new
    {
        @class=ViewData["class"]
    })

To prevent hardcoding the key/value pairs listed in EditorFor , convert the ViewData object to a Dictionary and pass that dictionary object to TextBox. 要防止对EditorFor中列出的键/值对进行硬编码,请将ViewData对象转换为Dictionary并将该字典对象传递给TextBox。

eg 例如

    @Html.EditorFor(m => m.DateModified, "Template", new { @class = "someClass", size=8 , htmlTag="custom" }) 

And in the template you have 在你的模板中

    @Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, ViewData.ToDictionary(c=>c.Key,c=>.Value))

To show json date in textbox (cshtml): 要在文本框中显示json日期(cshtml):

var d1 = ui.item.IssueDate;
var d = new Date(parseInt(d1.slice(6, -2)));
var Issdate = ("0" + (d.getMonth() + 1)).slice(-2) + '/' + 
              ("0" + d.getDate()).slice(-2) + '/' + 
              d.getFullYear().toString();
$('#IssueDate').val(Issdate);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM