Pete Davies
Pete Davies

Reputation: 1031

Insert Razor text into string from Model

Can anyone help me with some Razor syntax? I have a foreach loop ( shown at the end) and I would like to insert two items of text into a link to produce something like this:

<a href="/PDFFiles/Dummy.pdf#page=123" target="_blank">

I hope my question is clear, Many thanks.

@foreach (var item in Model)
  {
    <a href="/PDFFiles/**item.Filename**.pdf#page=**item.PageNum**" target="_blank">
  }

Upvotes: 0

Views: 4170

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

@foreach (var item in Model)
{
    <a href="@Html.AttributeEncode(@Url.Content("~/PDFFiles/" + item.Filename + ".pdf#page=" + item.PageNum))" target="_blank">Download</a>
}

Upvotes: 3

Brandon
Brandon

Reputation: 69953

Just prefix your variable name with @

@foreach (var item in Model)
{
  <a href="/PDFFiles/@(item.Filename).pdf#page=@(item.PageNum)" target="_blank">
}

Upvotes: 3

Related Questions