0

I have an MVC project that is mixed with TypeScript. I need my TypeScript object to obtain a string from my C# class.

I have a c# static class that produces a string from a StringBuilder. I have a typescript class that I need to be able to access this string. So in my constructor of the TypeScript class I would like to be able to:

this.element.append(MyCSharpStaticString);

Is there any way that I can do this ?

Relevant Code

cshtml

<div id="GridDiv"></div>

<script type="text/javascript">
    jQuery(function () {        
         new Jobber.OrdersGrid($('#GridDiv'), {}).init();

         Q.initFullHeightGridPage($('#GridDiv'));

    });
</script>

typescript :

export class OrdersGrid extends Serenity.EntityGrid<OrdersRow, any> {

protected getHeaderHtml(reportName) { return OrderHeaderHtml.HtmlHeader(reportName); }

       constructor(container: JQuery) {
            super(container);

          // Finds this element in the code and adds our HTML
          this.element.find('.grid-container').prepend(this.getHeaderHtml(""));

        }
}
9
  • are you using Razor? Commented Jan 3, 2017 at 17:59
  • I see nothing indicating what UI technology you're using, which will influence what answers you get. Someone added the asp.net-mvc tag, but it wasn't you, so you should clarify. Commented Jan 3, 2017 at 17:59
  • @MikeMcCaughan razor is a different tag (not added)... asp.net-mvc is server side, nothing to do with UI... btw you can use asp.net-mvc without razor Commented Jan 3, 2017 at 18:04
  • @SantiagoHernández I'm well aware of that, which is why I'm asking the OP to update their question with the appropriate view engine... Commented Jan 3, 2017 at 18:10
  • Possible duplicate of How to set javascript variables using MVC4 with Razor Commented Jan 3, 2017 at 18:10

1 Answer 1

0

If it does return a string you can bind it to your JavaScript method like:

<script>
this.element.append('@MyCsharpStaticString()');
</script>

Be sure you have included the model to your razor view.

Sign up to request clarification or add additional context in comments.

5 Comments

I think your answer is missing quotes: this.element.append('@MyCsharpStaticString');
There is nothing in the question that indicates Razor was being used. Is there a reason you assumed that and then edited the question based on your assumption?
@MikeMcCaughan hey, he said he had being using MVC With C# tag, i assumed razor involved. might have been a shot in the dark :)
@Valkyriee - while I have a razor file - client side can be updated and so I need the code to be in the client ts .. if it will go using razor I am open to trying it. BTW: cool moniker ..
@Ken i am afraid mate its impossible without razor, and thanks =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.