Marc Howard
Marc Howard

Reputation: 425

Using razor syntax within javascript

I'm trying to use the HTTP context razor method to map a server path within a javascript code block. However it's causing an exception with "illegal characters". I'm not sure which characters to encase it in, so razor see's it as c#.

    $(document).ready(function () {
    $('#StudentTableContainer').jtable({
        title: 'Asset Classes',
        paging: true,
        sorting: true,
        pageSize: 25,
        defaultSorting: 'Name ASC',
        actions: {
            listAction: '@(HttpContext.Current.Server.MapPath("GetAssetData?prod=funds"))'
        }
      });
    });

Upvotes: 0

Views: 126

Answers (1)

Kartikeya Khosla
Kartikeya Khosla

Reputation: 18883

There is no problem with razor syntax in Javascript ,you are sending querystring in server.mappath which is wrong :-

it should be like this :

 actions: {
            listAction: '@(HttpContext.Current.Server.MapPath("~/GetAssetData"))'
        }

instead of

actions: {
            listAction: '@(HttpContext.Current.Server.MapPath("~/GetAssetData?prod=funds"))'
        }

That is why "illegal characters" error is coming.

As far as i m understanding your question,i think you want to give path of a action in listAction then do it as :

actions: {
            listAction: '@(Url.Action("action name","controller name",new { prod="funds" }))'
        }

Upvotes: 2

Related Questions