X3074861X
X3074861X

Reputation: 3819

Dynamically Set list item Id

This seems like it should be really easy, but I cannot get this to work.

I have a collection of strings in my view model that's being outputted as list items :

<ul class="dropdown-menu" id="genreDropDown">
@foreach (string subject in Model.SuggestSubjects)
{
  <li id="listItem'@subject'"><a onclick="LoadSuggestTitles_BySubject('@subject');">@subject</a></li>
}
</ul>

When the page renders I'm seeing the id of the list items are being formatted as :

<li id="listItem'Mystery'">

My question :

Is there any way I can format the ID without the single quotes, so Razor can still pick up the value and append it to id?

Ultimately I'd like for it to read <li id="listItemMystery">.

Upvotes: 0

Views: 603

Answers (2)

John H
John H

Reputation: 14640

Try this:

@foreach (string subject in Model.SuggestSubjects)
{
  var id = "listItem" + @subject;
  <li id="@id"><a onclick="LoadSuggestTitles_BySubject('@subject');">@subject</a></li>
}

Upvotes: 1

acfrancis
acfrancis

Reputation: 3681

Replace:

<li id="listItem'@subject'">

With:

<li id="listItem@(subject)">

Upvotes: 1

Related Questions