Questions tagged [html-helper]

Refers to the `HtmlHelper` class for ASP.NET MVC views.

HtmlHelper is a helper class specific to ASP.NET MVC. The platform includes extension methods for this class that can be used to render HTML markup given the model and other inputs.

The canonical example is the DisplayFor method, which renders a display for a given model property. In Razor syntax, it looks like this, which renders the value of SomeProperty belonging to the current model class:

@Html.DisplayFor(model => model.SomeProperty)

The volume of methods available in HTML Helper is extensive, and they make use of data annotations (for string formatting and client-side validation). Their usefulness makes them a core part of the ASP.NET MVC framework.

2251 questions
53
votes
4 answers

I want to understand the lambda expression in @Html.DisplayFor(modelItem => item.FirstName)

I’m fairly new at C# and MVC and have used lambdas on certain occasions, such as for anonymous methods and on LINQ. Usually I see lambda expressions that look something like this: (x => x.Name), (x => { Console.WriteLine(x)) I understand that…
Jan Carlo Viray
  • 11,856
  • 11
  • 42
  • 63
51
votes
3 answers

ASP.NET MVC DropDownListFor with model of type List

I have a view with a model of type List and I want to place a drop down list on the page that contains all strings from the list as items in the drop down. I am new to MVC, how would I accomplish this? I tried this: @model…
CatDadCode
  • 58,507
  • 61
  • 212
  • 318
51
votes
6 answers

ASP.NET MVC HTML helper methods for new HTML5 input types

HTML5 appears to support a new range of input fields for things such as: Numbers Email addresses Colors URLs Numeric range (via a slider) Dates Search boxes Has anyone implemented HtmlHelper extension methods for ASP.NET MVC that generates these…
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
50
votes
7 answers

Razor: Declarative HTML helpers

I'm trying to write a simple declarative html helper: @helper Echo(string input) { @input } The helper works fine if I embed it into the page I want to use it on. But if I move it to a separate .cshtml file and place that file in the…
Adrian Grigore
  • 33,034
  • 36
  • 130
  • 210
50
votes
2 answers

Equivalent for MvcHtmlString in ASP.NET 5?

Is there an equivalent for MvcHtmlString in ASP.NET 5 ? Otherwise, how can we render HTML output from my custom HTMLHelper method properly ?
Christophe Gigax
  • 3,211
  • 4
  • 25
  • 37
49
votes
3 answers

What's the point of Html.DisplayTextFor()?

Is there a good reason to use the strongly typed html helper... <%: Html.DisplayTextFor(model => model.Email) %> As opposed to... <%: Model.Email %>
Sean Cain
  • 898
  • 2
  • 10
  • 16
47
votes
5 answers

Exclude/Remove Value from MVC 5.1 EnumDropDownListFor

I have a list of enums that I am using for a user management page. I'm using the new HtmlHelper in MVC 5.1 that allows me to create a dropdown list for Enum values. I now have a need to remove the Pending value from the list, this value will only…
Jak Hammond
  • 1,460
  • 3
  • 11
  • 24
46
votes
5 answers

MVC 3 - Html.EditorFor seems to cache old values after $.ajax call

This is a follow on from the following question: MVC 3 + $.ajax - response seems to be caching output from partial view There is a detailed description of the problem over there. However, I have now managed to narrow down the problem, that seems to…
awrigley
  • 13,481
  • 10
  • 83
  • 129
44
votes
6 answers

How to make the @Html.EditorFor Disabled

I have the following inside my asp.net mvc web application :-
Data Center Name @Html.EditorFor(model => model.Switch.TMSRack.DataCenter.Name, new { disabled = "disabled" })
but the field will not be disabled ,,…
John John
  • 1
  • 72
  • 238
  • 501
41
votes
4 answers

Html.ActionLink with a specified HTML id?

I'd like to give the like generated with an Html.ActionLink an HTML id so I can change the CSS depending on where I am. I have a MasterPage with a set of links and I'd like to distinguish the active "Tab" with Jquery changing the css of that active…
Peter
  • 685
  • 3
  • 9
  • 14
41
votes
2 answers

Difference between Html.RenderAction and Html.Action

Does anybody know what's the difference between Html.RenderAction and Html.Action?
Sasha
  • 20,424
  • 9
  • 40
  • 57
40
votes
5 answers

How do I generate a URL outside of a controller in ASP.NET MVC?

How do I generate a URL pointing to a controller action from a helper method outside of the controller?
Shawn Mclean
  • 56,733
  • 95
  • 279
  • 406
39
votes
2 answers

Handling onchange event in HTML.DropDownList Razor MVC

I'm handling an onchange event with a selected value by simple HTML like this: