Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net mvc adding css to editorfor?

I'd like to change style of the 'editor for' textbox from MVC, specifically I want to make the textbox larger.

I've tried adding css a few ways, to no avail.

including :

<td class="myCss"><%=Html.EditorFor(x => x.NickName)%></td>

and

<td class="myCss"><%=Html.EditorFor(x => x.NickName, new { @class = "myCss" })%></td>

help pls!

like image 533
hhay Avatar asked Aug 05 '10 20:08

hhay


People also ask

How do you style an EditorFor?

EditorFor does not allow for styling as there are no parameters for additional attributes. The reason for this is because the EditorFor doesn't always generate a single element as it can be overridden. To style a specific type of element you need to use the specific editor you want to use.

What is the difference between Textboxfor and EditorFor?

it's absolutly wrong answer, becuase the key difference is that Texbox returns input and editorfor returns your template where input is default template for editorfor.

How does HTML EditorFor work?

Simply put, the Html. EditorFor method allows the developer to retain control over the display of form elements by data type (ie. string, boolean, int…etc) or model attribute at a global level rather than at an individual view level. This allows for cleaner ASP markup and easily scalable form controls.


4 Answers

robh,

it's difficult to know from your question whether you're looking for a 'generic' or specific solution within your project. as such, i'm going to address the generic - works once, works everywhere solution.

this entails taking a few steps (convention over configuration). basically here's what's required:

  • create new folder under 'views->shared called Editor Templates'
  • create a new usercotrol (ascx) files under that called 'string.ascx'

now, define that ascx file as per:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" %>
<div class="editor-label">
    <%= Html.LabelFor(model => model) %>
</div>
<div class="new-editor-field">
    <%= Html.TextBoxFor(model => model) %>
    <%= Html.ValidationMessageFor(model => model) %>
</div>

this will now make all 'string' based EditorFor() calls use this 'template'. simply make the class 'new-editor-field' reflect your desired css style for that field. obviously, cook as per your own requirement (i.e. you may not want the LabelFor tat etc..)

hope this helps - tho i have to say, this is just one of a few ways to do this (but is my prefered way).

enjoy

jim

like image 59
jim tollan Avatar answered Sep 20 '22 05:09

jim tollan


Rather than requiring your input field to have the class directly applied, you could use the following:

<div class="editor-field myCss">
    @Html.EditorFor(model => model.Nickname)
</div>

Which results in roughly the following HTML

<div class="editor-field myCss">
    <input class="text-box single-line" data-val="true" id="Nickname" name="Nickname" type="text" value="">
</div>

Then just use the CSS rule to style appropriately

.myCss input.text-box {font-size: 2em;}

like image 37
davewasthere Avatar answered Sep 18 '22 05:09

davewasthere


Try this

<%= Html.TextBoxFor(x => x.NickName, new { @class = "myCss" })%>

or

<%= Html.TextAreaFor(x => x.NickName, new { cols = "40%", @class = "myCss" })%>

Now you can define your attributes because MVC knows what type to use (TextArea).

like image 21
Chase Florell Avatar answered Sep 22 '22 05:09

Chase Florell


Try this

@Html.EditorFor(p => p.Name, new { htmlAttributes = new{ @class ="my-css-class"} })
like image 27
Sóstenes Dias Jr Avatar answered Sep 19 '22 05:09

Sóstenes Dias Jr