1

I am trying to read model variable in jquery ready function.

 var jsNotes1 [email protected];

when the model.notes1 has an integer value, I was having no issues. But when the model.notes1 has a string value, say for ex: "abcd", the line is getting converted as below

var jsNotes1 = abcd

and Jquery is assuming abcd as a variable rather than as string value and is throwing reference error that abcd is not defined.

Please help me if i am missing something here

Mathew Thompson
56.5k15 gold badges130 silver badges151 bronze badges
asked Jun 11, 2015 at 12:31
1
  • 1
    String values need to be wrapped in quotes... Commented Jun 11, 2015 at 12:40

3 Answers 3

3

try to this

var jsNotes1 ='@Model.Notes1';
answered Jun 11, 2015 at 12:33

Comments

1

You need to wrap it in quotes as it needs to be treated as a string, otherwise it's invalid JavaScript

var jsNotes = "@Model.Notes1";
answered Jun 11, 2015 at 12:33

Comments

0

Try this is in javascript...

var jsNotes1= @Html.Raw(Json.Encode(Model.notes1));
answered Jun 11, 2015 at 12:53

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.