How to get html5 attributes and values ​​in mvc HiddenFor

I have a hidden input:

<input type="hidden" class="deleted" name="Deleted" data-id="@Model.Id" value="@Model.Deleted" />

I wanted to convert this to the MVC HiddenFor helper.

Got this:

@Html.HiddenFor(x => x.Deleted, new { @class="deleted" })

Thus, it covers the class. I also need the attribute and value of the data identifier.

Tried to add data id as:

@Html.HiddenFor(x => x.Deleted, new { @class="deleted", data-id="@Model.Id" })

Well, the helper doesn't seem to like the hyphen in the data id.

So how to get it?

Also how to get value="@Model.Deleted"there too?

+5
source share
2 answers

Use _, and MVC will convert it to -when rendering.

Also you do not need to @infront of Model.Id. Also remove the double quotes. The code below should work.

@Html.HiddenFor(x => x.Deleted, new { @class="deleted", data_id=Model.Id })

Why are you giving the css class to the hidden field?

+9
source

to try

@Html.HiddenFor(x => x.Deleted, new { @class="deleted", "data-id"="@Model.Id" })
-1

All Articles