20797

Simple html vs extension methods in Razor (preference)

Question:

For Simple tags in MVC Razor do you prefer to use simple HTML or use extension methods eg

<label for="male">Male</label> Or @Html.Label("male", "Male")

I feel it is sometime more easier to use the simple HTML. Extension methods make sense when you want to do some custom code.

Answer1:

Depends. If the male element you are associating this label to is rendered with a html helper like: @Html.TextBoxFor(x => x.Male) then I would use a Html.LabelFor and keep the lambdas and strong typing. Also I would never use:

@Html.Label("male", "Male")

I would use a view model and a strongly typed version:

@Html.LabelFor(x => x.Male)

and I would decorate my view model property with the [DisplayName] attribute so that I can control message on my view model:

[DisplayName("foo bar")] public string Male { get; set; }

So there are like many different possible scenarios. I could also sometimes simply use static HTML and no helpers (currently can't think of such scenario but I am sure it exists).

Answer2:

You could use DisplayFor. It's another way to show your data.

Recommend

  • Rails Returning ActiveRecord::Relation as json to an ajax call
  • python - regex why does `findall` find nothing, but `search` works? [duplicate]
  • How to pass data to a UIViewController from a UITableViewRowAction in Swift?
  • Azure deployment virtual directory [duplicate]
  • Build Linux Kernel module with warning i2c_register_board_info undefined
  • Extract images with podofo from pdf pages
  • github apit get commit info related to specific file
  • append to the same list with multiprocessing - python
  • Resources for learning Qlik Sense
  • Paypal IPN Listener Issue in C#
  • How to save a image coded in Data-URI?
  • ExecuteSQL doesn't select table if it having dateTime Offset value?
  • Disappearing icons in WPF
  • Is there a GetMouseMovePointsEx function in Lazarus?
  • Is it possible to use LinkShare signatures in an iOS Smart Banner?
  • SCNText Not Displaying
  • How do I add a trailing slash for Django MPTT-based categorization app?
  • Null Space Binary Matrix : Java
  • Pass multiple lines of stdin input to interactive Java command line program, non-interactively
  • what is “Other” category in CosmosDB monitoring graph?
  • android 3G signal strength
  • Raphael-GWT: fill image of a shape (Rect) appears offset. How to resolve this?
  • Magento Layered Navigation block. Move to center
  • SQL Worksheet is not displaying in SQL Developer
  • connect.cookieParser and connect.session
  • Adding Dynamic Row and Data on Checkbox Click
  • Validating my form with Jquery
  • Issue with Terrain Collision using Three.js
  • Find all parks for a given zipcode with google maps
  • Adding horizontal slider to QTableWidget
  • How to output data of primefaces DataGrid component by columns instead of by rows (transpose data)?
  • Query takes almost two seconds but matches only two rows - why isn't the index helping?
  • Magento-Change Attribute of All Products
  • Floating parent div grows to hypothetical width of floating child div
  • Content-Type alternative in MQTT
  • How to turn off notice reporting in xampp?
  • how to run ejabberd with Erlang on Heroku?
  • Stacked bar chart with continuous time-axis as x-axis
  • How to warp text around image in iOS?
  • Call Microservice from another Microservice within Docker