html.editorfor使用css,Add css class to Html.EditorFor in MVC 2

可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):

问题:

I'm trying to add a css class to a textbox. This is what I have in my view:

m.StartDate) %>

I tried following the instructions at this link by making my code:

m.StartDate, new { @class: "datepicker" }) %>

But I get a compiler error saying:

Syntax error, ',' expected

What am I doing wrong here?

回答1:

I would HIGHLY suggest using Editor Templates. It's definitely the "right" way to style your EditorFor.

You can tell a model property to use an Editor Template in two different ways.

The first (the simplest) is to create an editor template for a certain data type - DateTime for example.

The second way to do it is to set it declaratively in your DataAnnotations by using a UIHint.

Edit

I'd also like to add that you should use the "date" type in your input field so that even when JavaScript is disabled, your user can stills see a native datepicker (only valid on modern HTML5 browsers)

回答2:

With MVC3, I kept banging my head because I couldn't get this to work. I didn't want to create a whole EditorTemplate for just adding one class.

Well, instead of using EditorFor, use TextBoxFor, with of course the equals sign like so:

@Html.TextBoxFor(m=> m.ZipCode, new { @class = "zip" })

回答3:

I guess a quick and dirty way to do this would be in jQuery, yes?

$(document).ready(function () {

$('#StartDate').addClass('datepicker');

});

回答4:

Ideally, you should use the Editor Templates. I got around this issue by using the Editor Template inside the MvcHtmlString.Create() which will let you rebuild the actual HTML code. Of course, you'll want to copy everything in the "class" section to keep the Editor Template as useful as possible.

I tried many of the suggestions above, but eventually, I settled on this, because I think it's less complicated and it lets me continue using Editor Templates:

@MvcHtmlString.Create(Html.EditorFor(m => m.StartDate).ToString().Replace("class=\"text-box single-line\"", "class=\"text-box single-line datepicker\""))

回答5:

There is no overload for EditorFor that allows you to set HtmlProperties.

(IDictionary htmlAttributes)

This link explains how to do it:

http://aspadvice.com/blogs/kiran/archive/2009/11/29/Adding-html-attributes-support-for-Templates-2D00-ASP.Net-MVC-2.0-Beta_2D00_1.aspx

回答6:

I know this is an old question but thought I could contribute so here goes. I had the same problem and wanted to avoid making Editor Templates. I just wanted a generic handle everything solution that would allow me to specify html attributes when using Html.EditorFor in a view.

I really liked CIAs answer, but I expanded on it a bit so that you can pass in any attributes you need. I created an extra Html.EditorFor method that accepts html attributes:-

public static class EditorForExtentions

{

public static MvcHtmlString EditorFor(this HtmlHelper html, Expression> expression, Object htmlAttributes, bool extendAttributes)

{

string value = html.EditorFor(expression).ToString();

PropertyInfo[] properties = htmlAttributes.GetType().GetProperties();

foreach (PropertyInfo info in properties)

{

int index = value.ToLower().IndexOf(info.Name.ToLower() + "=");

if (index < 0)

value = value.Insert(value.Length - (value.EndsWith("/>") ? 2 : 1), info.Name.ToLower() + "=\"" + info.GetValue(htmlAttributes, null) + "\"");

else if (extendAttributes)

value = value.Insert(index + info.Name.Length + 2, info.GetValue(htmlAttributes, null) + " ");

}

return MvcHtmlString.Create(value);

}

}

You can call it in a view like this

m.StartDate, new { @class = "datepicker" }, true)%>

It uses the normal Html.EditorFor method to get the html string, then injects the html attributes needed.

回答7:

I was looking for a solution to apply a style to a specific box generated by the @HTML.EditorFor helper method.

The question was regarding setting a CSS class for @HTML.EditorFor but for anyone who wants to edit the style for a single element.. you can, for example, try this:

In my block, I added a style based on the ID generated by the helper:

..

#EnrollmentInfo_Format

{

width:50px;

font: normal 100% 'Lucida Grande',Tahoma,sans-serif;

font-size: 11px;

color: #2e6e9e;

}

and then in my page (i'm doing this in a partial view):

@Html.EditorFor(e => e.EnrollmentInfo.Format)

回答8:

Here's a very simple solution: Remove the double quotes from "datepicker" and retype them back into Visual Studio and it should work.

I had the same problem. I copied/pasted sample code from the web and the code had a special type of quote which caused the "," syntax problem. I know it's really not obvious.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值