INSERTING LITERAL VALUES ALONG WITH RAZOR INTO JAVASCRIPT

F Scott Fitzgerald

Total Post:29

Points:203
Posted by  F Scott Fitzgerald
 1547  View(s)
Ratings:
Rate this:
Hi Expert!

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()", 
                 }
  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: Inserting literal values along with razor into javascript

    Hi F Scott!


    Just try this:

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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!