Roberts Kalderauskis Roberts Kalderauskis - 6 months ago 14
AngularJS Question

Is there a way to pass string value to js file from View via function

I am trying to pass Model values from View to js via function. The problem is, i can pass integer, double values but not string. Is there a way to pass string to js via function?

Passing (cshtml file):

ng-submit="vm.updateProduct(@Model.Id, @Model.Name, @Model.Price)"


Receiving (js file):

vm.updateProduct = function (id, productName, productPrice)

Answer

Just add quotes around the string parameter. @Model.Name will only render the string value, so you need the quotes to make it valid javascript:

ng-submit="vm.updateProduct(@Model.Id, '@Model.Name', @Model.Price)"