Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you create html attributes with dashes when declaring html helpers in MVC?

How can I, for example, create a data-bind attribute when I declare and Html.TextboxFor helper?

Doing simply:

@Html.TextBoxFor(model => model.SomeProperty, new { data-bind="something" })

is not legitimate because of the naming issue with a dash "-" symbol. Is there a way around this issue or is it just not possible to pass html attributes with names containing dashes?

NOTE: I tried slapping the @ (this helps if you want to pass an atrribute that matches C# reserved words like "class") in front of the attribute but that didn't do the trick...

like image 980
Marko Avatar asked Nov 01 '13 15:11

Marko


People also ask

How can create HTML helper in ASP.NET MVC?

There are two ways in MVC to create custom Html helpers as below. We can create our own HTML helper by writing extension method for HTML helper class. These helpers are available to Helper property of class and you can use then just like inbuilt helpers. Add new class in MVC application and give it meaningful name.

What are HTML helpers in MVC?

In MVC, HTML Helper can be considered as a method that returns you a string. This string can describe the specific type of detail of your requirement. Example: We can utilize the HTML Helpers to perform standard HTML tags, for example HTML<input>, and any <img> tags.

How are inline HTML helpers defined in a Cshtml file to be used in the same view?

These are the type of helpers that are used on a single view and are used on the same page. inline HTML helpers can be created using @helper tag. <!

What is helper in HTML?

An HTML Helper is just a method that returns a string. The string can represent any type of content that you want. For example, you can use HTML Helpers to render standard HTML tags like HTML <input> and <img> tags.


1 Answers

You can use underscores (_) for that, MVC will convert them to dashes:

@Html.TextBoxFor(model => model.SomeProperty, new { data_bind = "something" })

Notice the data_bind property.

like image 122
Henk Mollema Avatar answered Oct 04 '22 03:10

Henk Mollema