Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add parameters to razor helpers

I want to know how write HTML Helper like @Html.TextBoxFor(model => model.signature) to have data-id parameter in produced input like below by helper.

<input type="text" name="signature"  data-id="No Signature" />

Note 1: parameter like dataId is work by htmlAttributes because it is a simple variable.

Note 2: I know extended method and use attribute like @{var attributes = new Dictionary<string, object>{{ "data-id", "No Signature" }};}

I feel that there must be a better way to solve this. Any idea...?

Thanks a lot.

like image 662
ehsan Avatar asked Oct 21 '22 00:10

ehsan


1 Answers

You can add data- attributes this way:

@Html.TextBoxFor(model => model.signature, new { data_id = "No signature" })

You have to use underscores (_) in stead of dashes (-).

Tip: it's also possible to use Model variables in your data- attributes:

new { data_id = Model.Id }
like image 186
Henk Mollema Avatar answered Oct 23 '22 15:10

Henk Mollema