Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get html5 attributes and values into mvc HiddenFor

Tags:

asp.net-mvc

I have following hidden input:

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

I was wanting to convert this to the MVC helper HiddenFor.

Got this far:

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

So that covers the class. I also need the data-id attribute and value.

Tried to add the 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 in there?

Also how to get the value="@Model.Deleted" in there also?

like image 796
AnonyMouse Avatar asked Aug 02 '12 03:08

AnonyMouse


1 Answers

Use _ and MVC will convert that to - when rendering.

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

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

And Why are you giving a css class to a hidden field ?

like image 134
Shyju Avatar answered Nov 29 '22 07:11

Shyju