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).
Just adding another option as this is what worked for me when trying to concat string and model value as id in an @html.ActionLink and also for the text value. I needed to use string.Concat. Don't know if this is bad from a performance point of view.
You want:
Razor will recognise the @ as the start of code, execute it and render the results in place in the attribute.
Edit:
This didn't work well as a comment, but here's a line from one of my Razor controls:
Try adding a hyphen (-) before the @. It's quite possible that Razor thinks it's an e-mail address and leaving it alone!
Try this (verified):