19

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!

3
  • possible duplicate of asp.net mvc 2 EditorFor() and html properties Commented Aug 5, 2010 at 20:22
  • Is EditorFor a "requirement" or are you open to trying TextBoxFor/TextAreaFor ? Commented Jul 13, 2011 at 17:18
  • I can't believe that you can't do this in a neat way! Commented Jan 18, 2013 at 15:49

6 Answers 6

8

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

Sign up to request clarification or add additional context in comments.

Comments

3

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;}

1 Comment

What if you're using a framework with pre-supplied Css classes.. you really need to be adding a class to the editorfor (so textboxfor seems to be the solution.)
2

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).

2 Comments

I downvoted 1 because he specifically said he wanted to change the EditorFor behaviour, not TextBoxFor.
I made a comment on the original question asking if EditorFor as a Requirement.
2

Try this

@Html.EditorFor(p => p.Name, new { htmlAttributes = new{ @class ="my-css-class"} })

Comments

0

Place result of EditorFor inside a div with some class attribute on it, and inside style definition for this class, use !important directive to force accept desired values to anything inside that div.

1 Comment

This did not work for me in MVC4. The style attribute on the DIV still does not take.
0

Try this,

https://stackoverflow.com/a/4249988/505474

Copied from above link,

@model DateTime?

@Html.TextBox("", (Model.HasValue ? Model.Value.ToShortDateString() : string.Empty), new { @class = "datePicker" })

It works for me...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.