1

Here I have an view model

public class SimpleModel { public string Id { get; set; } public string Name { get; set; } } 

And I passing the my model value,

... var myModel = new SimpleModel() { Id = "1234"; Name = "Dean"; }; return View(..., myModel); 

Here the problem I faced, I trying to passing the value to a javascript after pressing a button or link, but it fail to work, I doubt did my model contain the value and I double check by display it out, it seems it contain value.

Here the part of my code

... <a href="javascript:functionA(@Model.Name)">Call Function</a> <span>@Model.Name</span> ... 

If I remove the @Model.Name parameter, it work perfectly. Is there way to fix this? or I did wrong on the part I passing the parameter.

Error I get is

Uncaught SyntaxError: Invalid or unexpected token 

Any help to this will be appreciated and sorry if I explain the situation bad. Thanks.

2
  • 2
    You need to quote the value href="javascript:functionA('@Model.Name')" Commented Mar 23, 2018 at 3:37
  • @Jasen that what is missing, thanks for the help, save my days, thanks Commented Mar 23, 2018 at 3:57

1 Answer 1

0

dude, dont use ; when declaring, this is C# code and you should do it like this:

var myModel = new SimpleModel() { Id = "1234", Name = "Dean" }; 

and i believe this is C# not a javascript, this is ASP.NET, you put in wrong tag

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.