John Mayer
John Mayer

Reputation: 3363

Inserting literal values along with razor into javascript

I'm trying to insert quotes AND a value inside a javascript function. I'm doing this through a foreach method call.

@foreach (DateEarningsViewClass d in Model.dateEarnings) {
                 @d.date.ToShortDateString()
             }

The issue is that I want to insert around each date, but I get all sorts of errors when I attempt doing so. How can I do this?

What I essentially wanna display is:

@foreach (DateEarningsViewClass d in Model.dateEarnings) {
                     "@d.date.ToShortDateString()", 
                 }

Upvotes: 2

Views: 751

Answers (2)

Andrei
Andrei

Reputation: 44660

Just try this:

@foreach (DateEarningsViewClass d in Model.dateEarnings) {
   @Html.Raw("\"" + d.date.ToShortDateString() + "\"")
}

Upvotes: 4

Jacques Snyman
Jacques Snyman

Reputation: 4300

You can use the tag.

@foreach (DateEarningsViewClass d in Model.dateEarnings) {
    <text>"</text>
    @d.date.ToShortDateString()        
    <text>"</text>
}

Upvotes: 2

Related Questions