2

I essentially want to pass a multi-line string to a function from inside of a Razor/cshtml view.

I was hoping I could use <text> to accomplish this.

Here's what I'm trying:

        @Js.Tsc(@<text>
class Greeter {
    greeting: string;
    constructor (message: string) {
        this.greeting = message;
    }
    greet() {
        return "Hello, " + this.greeting;
    }
}   

var greeter = new Greeter("world");

var button = document.createElement('button')
button.innerText = "Say Hello"
button.onclick = function() {
    alert(greeter.greet())
}

document.body.appendChild(button)
        </text>.ToString())

But I still get

Compiler Error Message: CS1660: Cannot convert lambda expression to type 'string' because it is not a delegate type

on the @Js.Tsc(@<text> line.

I thought the .ToString() would have cast it properly. Is there a way to do this?

I know I can use @"-style strings, but that makes writing the code in between more complicated.

mpen
  • 272,448
  • 266
  • 850
  • 1,236

1 Answers1

1

Razor is still C# code, so I'm thinking you would pass the script as you would any other string parameter. But you'll have to escape any special characters, such as the double quotes. And I don't think you need the @<text>.

@Js.Tsc("class Greeter {
    greeting: string;
    constructor (message: string) {
        this.greeting = message;
    }
    greet() {
        return \"Hello, \" + this.greeting;
    }
}   

var greeter = new Greeter(\"world\");

var button = document.createElement('button')
button.innerText = \"Say Hello\"
button.onclick = function() {
    alert(greeter.greet())
}

document.body.appendChild(button);")
mgnoonan
  • 7,060
  • 5
  • 24
  • 27
  • Yes.. I commented on that in my last sentence. I was hoping to avoid having to escape all the quotes in my JS. – mpen Dec 27 '12 at 18:35
  • Can you split the script to a separate file, then pass a StreamReader to the function instead of a string? That would save the escaping task, but you'd be forced to live with a more strict development protocol. – mgnoonan Dec 27 '12 at 18:45
  • Nope. That completely defeats the purpose of what I'm trying to do here. If I had my script in a separate file I'd just run it through the normal typescript handler. I wanted to mix in some C# variables from the view I'm working in, which I can't do if it's a separate script file. – mpen Dec 27 '12 at 18:48
  • Not that I'm advocating this, just trying to find an answer that suits you, but you could come up with a token replacement convention (similar to string.Format), read the file from disk or URI, and pass the replaced string to the Tsc() method. – mgnoonan Dec 27 '12 at 20:49
  • You mean to sub-in my C# vars? If I were to go down that route I might want to instead add some query params to the script request, e.g., `` and then create a special request handler to process the file. – mpen Dec 27 '12 at 21:06
  • I think those may be your choices. Can't say I'm a fan of any of them. – mgnoonan Dec 27 '12 at 21:53