How to add data-autocomplete HTML attribute attribute to TextBoxFor HTML Helper?

autocomplete text field to get the autocomplete text field of the city.

My code looks like this:

<input id="location" type="text" name="q" 
data-autocomplete="@Url.Action("locationSearch", "Home", 
                       new { text = "location" })"/>

Now I want to convert this to a syntex razor. I tried this but did not work.

@Html.TextBoxFor(model => model.Location, 
   new { data-autocomplete = Url.Action("locationSearch", "Home")})

How can I solve this problem?

+5
source share
3 answers

data-autocomplete is an HTML attribute. First of all, you cannot use dashes when specifying attributes in MVC, so you need to replace your autocomplete with autocomplete data. MVC is smart enough, and the end result will read autocomplete data.

HTML , HTML-:

@Html.TextBoxFor(model => model.Location, new { data_autocomplete = Url.Action("locationSearch", "Home") })

, , .

+7

u

<select id="location" name="location"></select>

           <input type="submit" value="Send">

dropdownlist, , css

+1

You must use the undescore character in the HtmlAttributes argument:

@Html.TextBoxFor(model => model.Location, new { data_autocomplete = Url.Action("locationSearch", "Home")})
0
source

All Articles