简体   繁体   English

向EditorFor添加一个类 - razor c#

[英]adding a class to EditorFor - razor c#

(The editor is for a DateTime property called "ADate" (编辑器用于名为“ADate”的DateTime属性

I am trying this but it does not work. 我正在尝试这个,但它不起作用。

@Html.EditorFor(model => model.ADate, new { cssClass = "date" } )

So I tried this: 所以我尝试了这个:

@Html.TextBoxFor(model => model.ADate, new { @class = "date" })

but it outputs type = text.. ofcourse it does. 但它输出type = text .. ofcourse它确实。

So I tried a template... I added a folder in shared: 所以我尝试了一个模板......我在共享中添加了一个文件夹:

Shared/EditorTemplates

and then I created a .cshtml partial view called Date.cshtml 然后我创建了一个名为Date.cshtml的.cshtml局部视图

Now what on earth do I put inside it :O... 现在我在里面放什么:哦......

I have tried to understand lots of posts and stack overflow entries but it's not sinking in. 我试图了解很多帖子和堆栈溢出条目,但它并没有陷入其中。

The goal is to attach a datepicker to the class ".date" across the entire app where ".date" class is used... The TextBoxFor works with my adding class part... but as I said, the type changes from date to text :( 目标是在整个应用程序中使用“。日期”类附加一个日期选择器 “.date” ...使用“.date”类... TextBoxFor适用于我的添加类部分...但正如我所说,类型从日期更改到文本 :(

Ok so this is what you can do: 好的,这就是你可以做的:

In your EditorTemplates folder create a template called DateTime.cshml (the name will resolve automatically if you use it for dates, otherwise you have to specify it when using it). EditorTemplates文件夹中,创建一个名为DateTime.cshml的模板(如果您将该名称用于日期,该名称将自动解析,否则您在使用它时必须指定该名称)。 Then in it: 然后在其中:

@model System.DateTime

@Html.TextBox(
   string.Empty, 
   Model.ToString("yyyy-MM-dd"),
   new { @class="datepicker", @type = "date"})

Using the last parameter you can specify any html attribute (class, data, type etc.). 使用最后一个参数,您可以指定任何html属性(类,数据,类型等)。

Then you use it like this: 然后你像这样使用它:

@Html.EditorFor(x => x.ADate)

In case you chose to name your template with a different name you can specify it by invoking another overload: 如果您选择使用不同的名称命名模板,可以通过调用另一个重载来指定它:

@Html.EditorFor(x => x.ADate, "MyOtherAwesomeTemplate")

The EditorFor html helper does not render a date picker for your DateTime attributes (if that's what you want to do). EditorFor html帮助器不会为DateTime属性呈现日期选择器(如果这是您想要做的)。 The default EditorFor for DateTime is a text input. DateTime的默认EditorFor是文本输入。 If you want to use a date picker, you'll have to use jQuery DatePicker (or any other third party date picker). 如果要使用日期选择器,则必须使用jQuery DatePicker(或任何其他第三方日期选择器)。

Also, the EditorFor helper does not have a parameter for html attributes. 此外,EditorFor助手没有html属性的参数。 If you want to assign a class, you'll have to use TextBoxFor. 如果要分配类,则必须使用TextBoxFor。

In your main View, use the EditorFor like this: 在主视图中,使用EditorFor,如下所示:

@Html.EditorFor(model => model.ADate)

Then, in your Editor Template (Date.cshtml), you'll have: 然后,在您的编辑模板(Date.cshtml)中,您将拥有:

@model System.DateTime

<script type="text/javascript">
    $(function () {
        $(".date").datepicker();
    });
</script>

@Html.TextBox("", Model.ToString("d"), new { @class = "date" })

You can download the jQuery UI from here: Download jQuery UI 您可以从此处下载jQuery UI下载jQuery UI

And, you can learn more about the jQuery DatePicker here: jQuery DatePicker 并且,您可以在此处了解有关jQuery DatePicker的更多信息: jQuery DatePicker

You can do this 你可以这样做

@Html.EditorFor(model => model.ADate)

<style type="text/css">

#ADate
{
 @* your css properties goes here *@
}
</style>

this works fine for MVC3 Razor 这适用于MVC3 Razor

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

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