-2

I have the following code in my View:

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

I have the following code in my ViewModel:

     [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy/MM/dd}")]
     public DateTime? DtCollectedLead { get; set; }

What is happening is that the width does not adjust to 100px. I know I can use TextBoxFor bu then I cannot take advantage of the the DisplayFormat that I need for the date field. Is there any way to adjust the width for EditorFor?

Nate Pet
  • 44,246
  • 124
  • 269
  • 414

1 Answers1

0

You would need to add an EditorFor template and reference that in your usage of EditorFor.

The object you are passing (new { style="width:100px;" }) adds data to the ViewData object that can be accessed.

http://msdn.microsoft.com/en-us/library/ee407414(v=vs.118).aspx here is the overload you could use.

so you could do the following:

  @Html.EditorFor(model => model.DtCollectedLead, "CustomDateTime")

and then you would have a partialview file in the EditorFor folder called CustomDateTime. you would set the Model of the partialview as a DateTime, which allows you to add the Html you require.

Additionally, if you want the partialview EditorFor template for all DateTime models. you could call the partialview file DateTime and this will be picked up.

Mike
  • 2,547
  • 1
  • 24
  • 36