MVC3,编辑模板,CSS CLAS,最大长度和大小 [英] mvc3, editor template, css clas, maxlength and size

查看:150
本文介绍了MVC3,编辑模板,CSS CLAS,最大长度和大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个编辑模板下面,但类,最大长度和大小的属性没有得到到源。

I have an editor template as following but class, maxlength and size attributes are not getting to the source.

@using System.Globalization
@model DateTime?
@Html.TextBox("", (Model != null && Model.HasValue && !Model.Value.ToString(CultureInfo.InvariantCulture).Contains("1900") && !Model.Value.ToString(CultureInfo.InvariantCulture).Contains("0001") ? Model.Value.ToString("MM/dd/yyyy") : string.Empty), new { @class = "datePicker", maxlength = "12", size = "12" })

我已经把它改成下面的,它仍然是相同的。

I have changed it to following and it is still the same

@Html.EditorFor(x => x.Criteria.FromDate, new { @class = "datePicker", maxlength = "12", size = "12" })

来源

<input class="text-box single-line" id="Criteria_FromDate" name="Criteria.FromDate" type="text" value="" />

我怎样才能解决这个问题?

How can i fix this?

推荐答案

请确保您的编辑器模板名为日期时间 - 放置在文件夹视图/共享/ EditorTemplates,和你的模型(Criteria.FormDate)是的同一类型作为EditorTemplate模型(日期时间?)。

Make sure your Editor template is named DateTime - placed in folder Views/Shared/EditorTemplates, and your model (Criteria.FormDate) is same type as EditorTemplate model (DateTime?).

如果所有的日期时间字段将拥有相同的最大长度和大小,你可以让他们很难codeD放入EditorTemplate。
例如,对于你的HTML:

If all DateTime fields will have same maxlength and size you can keep them hardcoded in your EditorTemplate. Example for your html:

@EditorFor(x => x.Criteria.FormDate) //no need to pass html attributes object if they are not used in the editor template

- 它值得尝试 @EditorFor(型号EditorTemplateName)明确地说你想要的TemplateEditor为传递模型。这是当你有相同型号的多个编辑器的情况下,让你显式调用它们(就像调用局部视图和模型传递给它)。

-- its worth trying @EditorFor(model, "EditorTemplateName") to explicitly say you want that TemplateEditor for passed model. This is the case when you have multiple editors for same model type, so you call them explicitly(works like calling partial view and passing model to it).

编辑:
看你的模板后,在我看来,你的Criteria.FormDate非空。你应该看看改善/模板重构你的code。

After looking at your template, it seems to me that your Criteria.FormDate is non nullable. You should look at improving/refactoring your code in template.

这篇关于MVC3,编辑模板,CSS CLAS,最大长度和大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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