Alex Rivera | Logout

Is there a way to concatenate strings in html attributes?

Asked 2011-07-12T21:03:36.167
31

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).

Edit
Report

1 Answer

68

Try this (verified):

<div id="@("label"+Model.Id)" class="display-field">@Model.InitValue</div>
answered 2011-07-12T21:05:44.927

Your Answer