jimmy
jimmy

Reputation: 756

ASP .Net MVC page set ID of @Html.CheckBoxFor using model data

I'm trying to set the id of a @Html.CheckBoxFor using data from the page model.

@Html.CheckBoxFor(modelItem => item.IsOpen, htmlAttributes: new { @class = "custom-control-input switcher", @disabled = "disabled", @id = "[email protected]" })

The last part "[email protected]" is not working, I would like it to render: id="cbxOpen_1" where 1 is the item id. But it obviously renders as a string e.g. id="[email protected]"

Is there a way to make it use the data?

Upvotes: 1

Views: 644

Answers (2)

John H
John H

Reputation: 14640

Just handle it as you would in C# normally:

@id = $"cbxOpen_{item.Id}"

Upvotes: 0

Drew
Drew

Reputation: 4373

Change this part @id = "[email protected]" to @id = "cbxOpen_" & item.Id

Upvotes: 2

Related Questions