0

How to set textbox width from mvc3 model , suppose my model is following ,

public class MVCMODEL
{
    [Required] 
    public  int Name{ get; set; }
}

i want that when this textbox renders in view its width will be 800px how to add custom styles in model ?

Thanks ,

5 Answers 5

3

You can't do this from a model. In MVC, a model is a representation of the data structure that corresponds to the information you are sending to and from the page. The very point of MVC is to separate the aesthetics of the site from the logic that actually makes it run. In order to set visual aspects of a view you need to alter the view's CSS.

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

Comments

1

You can't do this because it is a specific layout behavior, so you should set the width at the View as you need. Something like this would solve yoru problem:

@Html.TextBoxFor(model => model.Name, new { style = "width: 800px;"})

Comments

1

Although you could add your own DataAnnotation type attribute extensions and decorate your model / view model properties accordingly (as per this blog post), this would be a horrid pollution of concerns IMO.

Somewhat better would be to use a similar technique and instead of rendering hard coded widths, instead decorate your model properties with a CSS class and then use CSS to control the formatting accordingly. However, purists may still still disagree - even applying a CSS class to a view model could be crossing the line.

e.g.

[HtmlProperties(CssClass="LargeTextBox")]
[Required] 
public  int Name{ get; set; }

with the idea that the rendered Html is

<input type="text" value="" name="Name" id="Name" class="LargeTextBox"/>

The actual widths would be controlled in an appropriate .css

Comments

0

Agree with all the other answers... you can also define the max lenght of your field in the Model as:

public class MVCMODEL
{
    [Required] 
    [MaxLenght(300)] // number of characters allowed in this field.
    public  int Name{ get; set; }
}

Comments

0

you may write somthing like this .

:<%=Html.TextBoxFor(m => m.Name, new { size=4})%>

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.