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

5 Answers 5

68

Try this (verified):

<div id="@("label"+Model.Id)" class="display-field">@Model.InitValue</div>
Sign up to request clarification or add additional context in comments.

3 Comments

This worked: <div id="@("label"+Model.Id)" class="display-field">@Model.InitValue</div> Thanks a lot.
Added quotes just now (while you're posting your comment) :)
Yeah, I saw. Thanks for the help, though. I'm still new to MVC3 and Razor syntax.
4

You want:

<div id="[email protected]" ...

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:

<input type="text" readonly="readonly" 
       class="display-field [email protected]" 
       id="@((ViewData["id"] == null) ? 
         ViewData.ModelMetadata.PropertyName : ViewData["id"])"
       value="@Proj.GetJobStatusValue(Model)" />

Try adding a hyphen (-) before the @. It's quite possible that Razor thinks it's an e-mail address and leaving it alone!

1 Comment

I tried that. In the HTML source in my browser, the id is simply "[email protected]"
2

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.

  @Html.ActionLink(string.Concat("View all (", @Model.FooCount, ")"),
        //actionName        
        "SeeAllFoos",
        //ControllerName
        "Foo",
        // routeValues
        new { FooId = @Model.Foo.id },
        //htmlAttributes
        new { @class = "btn btn-success", onclick = "ShowProgress();",
        id = string.Concat("Foo",@Model.Foo.id.ToString()) })

Comments

1

Here is what you need to do.

id="[email protected]"

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

Comments

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

Adding underscore(_) id="[email protected]" or hyphen (-) id="[email protected]" to separate could also help. Have a great day!

1 Comment

Welcome to Stack Overflow! While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.