3

I am working on an asp.net mvc-5 web application , and i wrote the following :-

@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
&
@Html.EditorFor(model => model.Name, new { @class = "form-control"  })

but this will not have any effect on the generated html, but if i changed the EditorFor to be TextboxFor i will get the effect for the form-control class ? can anyone advice on this please ? i read that this is supported only inside asp.net mvc 5.1 ? so what are the available approaches i can follow to get this working other than upgrading my asp.net mvc-5 to asp.net mvc 5.1 , to eliminate the risk of upgrading? Can anyone adivce ?

John John
  • 1
  • 72
  • 238
  • 501
  • 1
    Have you looked at this post: http://stackoverflow.com/questions/4576209/asp-net-mvc-3-razor-adding-class-to-editorfor – David Tansey Jan 16 '15 at 01:19
  • so how i can modify assign the class inside the editor template ?? – John John Jan 16 '15 at 02:31
  • Can you show your editor template code? The accepted answer on that post I referenced (with 106 upvotes!) indicates that specifying 'the class' for an Editor template _does not make sense_ because the template may contain many pieces of markup (in contrast to a TextboxFor which renders only one tag). The answer also shows _how to apply a class to a tag within the Editor Template_. Which part is not clear? – David Tansey Jan 16 '15 at 02:48
  • @johnG, If you want to specify the class in the `EditorFor()` method, you can pass it as `AdditionalViewData` as shown in [this answer](http://stackoverflow.com/questions/27341031/adding-class-to-editorfor-in-mvc/27341257#27341257) –  Jan 16 '15 at 04:27
  • @StephenMuecke thanks for the reply, but seems that if i want to use "htmlAttributes " inside the editorfor i will need to upfrage my project from mvc5.0 to mvc5.1 or to mvc5.2 ... – John John Jan 16 '15 at 11:23
  • @DavidTansey but from the replies yo can see that changing the EditorFor to be TextBoxfor will raise an issue that TextBoxFor doesn't honor the DisplayFormat set. !! so that why i do not want to modify the EditorFor template, to be TextBoxFor! – John John Jan 16 '15 at 11:26
  • @johnG, No you don't (although you can) - the answer in the link shows how do do it with MVC-4 –  Jan 16 '15 at 11:28
  • @johnG, and further to you comment to David, you can also use `@ViewData.TemplateInfo.FormattedModelValue` to get the value defined by the `[DisplayFormat]` attribute –  Jan 16 '15 at 11:33
  • @StephenMuecke now i am confused , inside the link you mentioned to use "new { htmlAttributes }" inside Editorfor but this can not be done inside asp.net mvc5 and i need to update to mvc1.0 ... because inside mvc5.0 it will raise an error that can not add new { htmlAttributes} to editorfor .. not sure what do you mean the link show how to do it in mvc4? can u advice ? – John John Jan 16 '15 at 16:29
  • @johnG, I was referring to the link I provided in my first comment - **[this one](http://stackoverflow.com/questions/27341031/adding-class-to-editorfor-in-mvc/27341257#27341257)**. In any case I have added an answer that you can use in MVC-5.0 with some options you can consider –  Jan 17 '15 at 03:02

2 Answers2

4

Yes, to pass html attributes to a standard EditorFor() you need MVC-5.1+. If you want to replicate this with MVC-5, you can create a custom editor template and pass the attributes using the overload that accepts additionViewData. For example, create a new EditorTemplate named "String.cshtml" to apply the template for all properties that are typeof string

/Views/Shared/EditorTemplates/String.cshtml

@Html.TextBoxFor(m => m, ViewData["attributes"])

and in the view

@Html.EditorFor(m => m.Name, new { attributes = new { @class = "form-control" } })

or create a specificEditorTemplate

/Views/Shared/EditorTemplates/MyCustomTemplate.cshtml

@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, ViewData["htmlAttributes"])

and in the view

@Html.EditorFor(m => m.Name, "MyCustomTemplate", new { attributes = new { @class = "form-control" } })

The second example shows how to respect the DisplayFormat attribute as mentioned in your comments above, for example

[DisplayFormat(DataFormatString="{0:C}", ApplyFormatInEditMode = true)]
public decimal Amount { get; set; }

will format the value as a currency string.

This answer also gives some other options including creating a custom html helper for rendering bootstrap controls

Community
  • 1
  • 1
  • thanks for the reply, seems the easiest way is to upgrade my mvc5.0 to mvc 5.2.2 . but is upgrading from asp.net mvc5 to mvc5.2.2 , include running the following command Install-Package Microsoft.AspNet.Mvc -Version 5.2.2 ? or there are other manual steps? – John John Jan 19 '15 at 12:26
  • 1
    As far as I know the Nuget package should update the web.config files correctly (worked for me for 5.0 > 5.1 but I haven't done 5.2 yet). Check the prerequisites [here](http://blogs.msdn.com/b/webdev/archive/2014/07/02/announcing-the-release-of-asp-net-mvc-5-2-web-api-2-2-and-web-pages-3-2.aspx) so you dont have the problems described [here](http://stackoverflow.com/questions/21343714/mvc-5-mvc-5-1-migration-intellisense-issues). As always, make sure you have a backup :) –  Jan 19 '15 at 22:52
  • thanks for the reply. now i am not using webAPi so i do not need to update it , is this correct ? or it is better to update both asp.net mvc to 5.2.2 and webapi to 5.2.2 and also the Microsoft.AspNet.WebPages to Version 3.2.0? – John John Jan 20 '15 at 00:01
4

This works:

@Html.EditorFor(x => x.Created, new { htmlAttributes = new { @class = "date" } })
ransems
  • 641
  • 7
  • 19