Reputation: 2915
I am trying to do the following:
@foreach(var p in @Model.line_products){
<img class="[email protected]_products[i].short_name" src="/Content/images/@Model.line_products[i].image_name" />
}
Which isn't working, it renders the text just the way it is, not recognizing the '@'. I found this other post in Stackoverflow, which suggests adding parenthesis in the following way:
@foreach(var p in @Model.line_products){
<img class="small_img_(@Model.line_products[i].short_name)" src="/Content/images/@Model.line_products[i].image_name" />
}
Using this workaround, I get that my id is rendered as small_img_(MODEL ATTRIBUTE). Isn't there a workaround which doesn't require adding specific characters? (such as the parenthesis).
Upvotes: 1
Views: 805
Reputation: 2210
I sometimes put a couple of Guids in the id of an element and an underscore separator doesn't work.
There are two ways around this. First use the entity code _
instead and secondly just use a hyphen.
<input id="[email protected]_@ing.Ingredient.Guid" type="checkbox" class="chk_Done form-check">
<input id="[email protected]@ing.Ingredient.Guid" type="checkbox" class="chk_Done form-check">
This is because I want to grab out the Guid's when the check box is clicked with some JQuery like this:
$(".chk_Done").click(function () {
var obj =[];
const itemId = ($(this).attr("id"));
const myArray = itemId.split("_");
var ClassLeaderGuid = myArray[1], IngredientGuid = myArray[2];
Upvotes: 0
Reputation: 23083
You have more errors than a simple undercore problem here. You cannot use @Model inside your if. You are already in a @ block. Simply use @foreach(var p in Model.line_products)
.
Plus, the way you wrote the parenthesis, they will get rendered. What you want is
small_img_@(Model.line_products[i].short_name)
Upvotes: 4
Reputation: 700152
Put the parenthesis after the @
instead of before:
class="small_img_@(Model.line_products[i].short_name)"
Upvotes: 2