Question

I have an issue which seems ridiculous. I'm trying to set the WebApi base service url from my controller and pass it to the front end / jquery for use in the KendoGrid. I can't seem to pass a URL in a string variable from within a model to my jquery var without getting an error, such as:

ReferenceError: invalid assignment left-hand side
var crudServiceBaseUrl = "http://localhost:55607/api/StockSelectorWebApi?" + sea...

and if I UrlEncode it:

SyntaxError: identifier starts immediately after numeric literal
var crudServiceBaseUrl = http%3a%2f%2flocalhost%3a55607%2fapi%2fStockSelectorWeb...

my query is as follows:

    var crudServiceBaseUrl = @Model.CrudBaseServiceUrl;

    var dataSource = new kendo.data.DataSource({
        transport: {
            read: {
                url: crudServiceBaseUrl,
                dataType: "json"
            }
        },
        batch: true,
        pageSize: 20,
        schema: {
            model: {
                id: "WedNo",
                fields: {
                    WedNo: { editable: false, nullable: true, type: "number" },
                    MillNo: { editable: false, nullable: true, type: "string" },
                    Location: { editable: false, nullable: true, type: "string" },
                    Stockholder: { editable: false, nullable: true, type: "string" },
                    Auths: { editable: false, nullable: true, type: "string" }
                }
            }
        }
    });

and my model:

model.CrudBaseServiceUrl = Server.UrlEncode("http://localhost:55607/api/StockSelectorWebApi?searchType=Arrival&parameterOne=" + model.Arrival);

EDIT: Oddly enough I also had tried

model.CrudBaseServiceUrl = "'URLHERE'";

and

model.CrudBaseServiceUrl = "'" + "URLHERE" + "'";

But neither worked, but as was pointed out below (in the accepted answer) just do:

model.CrudBaseServiceUrl = "URLHERE";

and then

var crudServiceBaseUrl = '@Model.CrudBaseServiceUrl';

pffttt

Was it helpful?

Solution

var crudServiceBaseUrl = '@Model.CrudBaseServiceUrl'

basically, if you use string variables coming from the server within javascript you should use quotes around them. no need to encode or anything.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top