简体   繁体   中英

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"))'
        }
      });
    });

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 im 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" }))'
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM