Is there a way to concatenate strings in html attributes?

Viewed 59936

I'm using MVC3 and I wanted to use a partial view to create dynamic DOM elements. This is my current partial view:

@model MVCApp.ViewModels.TitlesViewModel

<div class="display-label">Name</div>
<div id="label"+"@Model.Id" class="display-field">@Model.InitValue</div>

Model.Id is 1 but in the HTML in the browser, I currently get:

id="label"+"1"

So if I try and do something like:

alert($("#label1").text())

There's an alert box with nothing in it.

So how can I add the two strings together to form one coherent string that is recognized by jQuery (or document.getElementByID(str) for that matter).

5 Answers

Here is what you need to do.

id="label_@Model.Id"

Underscore(_) is required.For me passing id without Underscore made an issue. Happy conding.

id="@("label")@Model.Id"

Adding underscore(_) id="label_@Model.Id" or hyphen (-) id="label-@Model.Id" to separate could also help. Have a great day!

Related