-1

I've create MVC5 App which generate from my model by scaffold the view and controller currently the default size of the text box are 18 character and I want it to be 5 ,how should I reduce the default size of the text box?

<div class="form-group">
    @Html.LabelFor(model => model.num, new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.Nun)
        @Html.ValidationMessageFor(model => model.Num)
    </div>
</div>

I've tried without success with

  @Html.EditorFor(model => model.Nun, new { @style = "width: 100px;" })
07_05_GuyT
  • 2,787
  • 14
  • 43
  • 88

3 Answers3

1

The reason that you can not do

@Html.EditorFor(model => model.Nun, new { style = "width: 100px;" }) 

is because EditorFor could contain more than one object. As a result, you would be applying properties to things which may be incorrect in the case that you may be setting an ID as well as a style.

So, there are a couple of ways in which you can style the boxes.

This CSS will do it for you: (you can use just 1 line or all of them which apply)

.col-md-10 input                 { width:50px }     /* <input> */    
.col-md-10 input[type=text]      { width:50px }     /* <input type="text"> */
.col-md-10 input[type=password]  { width:50px }     /* <input type="password"> */
.col-md-10 textarea              { width:50px }     /* <textarea> */

So for example, any <input type="text"> which is a child element of .col-md-10 will be set to 50px wide.

or you can instead use TextBoxFor which will allow you to add an inline style:

@Html.TextBoxFor(model => model.Nun, new { style = "width: 50px;" })
wf4
  • 3,727
  • 2
  • 34
  • 45
0

In model do like this for Nun property:

[StringLength(6)]
public string Nun {get;set;}

if you want to change width then:

@Html.TextBoxFor(model => model.Nun,new {style="width:100px" })
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
0

Use TextBoxFor

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

Check this LINK . If you dont want to use TextBoxFor instead of EditorFor

Community
  • 1
  • 1
Nitin Varpe
  • 10,450
  • 6
  • 36
  • 60