I'm looping through a list of vendors on my page, creating a checkbox for each one. I'd like to have each checkbox have an ID like "Vendor1", "Vendor30", where the number is the ID of the vendor.
I have this right now:
@foreach (var vendor in Model.Vendors) {
<input id="@vendor.VendorID" type="checkbox" name="vendors" value="@vendor.VendorID" />
}
But that just gives me a number for the ID. I want to add "Vendor" to the front of it.
If I do this:
@foreach (var vendor in Model.Vendors) {
<input id="[email protected]" type="checkbox" name="vendors" value="@vendor.VendorID" />
}
Every checkbox in the list has an id of "[email protected]".
I'm not using @Html.CheckBox
because of the hidden field that comes along with it, and several other problems it was causing that I didn't include in this example.
Try wrapping the razor expression in parantheses:
<input id="Vendor@(vendor.VendorID)" type="checkbox" name="vendors" value="@vendor.VendorID" />
I'm not using @Html.CheckBox because of the hidden field that comes along with it, and several other problems it was causing that I didn't include in this example.
Personally I've never had any problems with it and would recommend using it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With