admin管理员组文章数量:1410737
I have to set a Javascript variable to equal a property in my MVC model.
I am doing this to detect if any changes were made to a textbox, this is setting the "original" value.
My Javascript code looks like this:
var initVal = '@Model.ReferralHistoryDetail[1].ReferralComments';
I am getting an error, which I assume is due to this containing carriage returns in the ments.
Uncaught SyntaxError: Unexpected token ILLEGAL
The HTML being rendered in this case is putting the closing quote on a newline, and that is the error being shown in the developer console.
For example, the rendered HTML is:
var initVal = 'blah blah blah
';
What is the proper way to handle this?
I have to set a Javascript variable to equal a property in my MVC model.
I am doing this to detect if any changes were made to a textbox, this is setting the "original" value.
My Javascript code looks like this:
var initVal = '@Model.ReferralHistoryDetail[1].ReferralComments';
I am getting an error, which I assume is due to this containing carriage returns in the ments.
Uncaught SyntaxError: Unexpected token ILLEGAL
The HTML being rendered in this case is putting the closing quote on a newline, and that is the error being shown in the developer console.
For example, the rendered HTML is:
var initVal = 'blah blah blah
';
What is the proper way to handle this?
Share Improve this question asked Nov 12, 2015 at 14:42 PatrickPatrick 5,90616 gold badges67 silver badges106 bronze badges 2- Do you want it to have that CRLF? – Jamie Rees Commented Nov 12, 2015 at 14:48
- Yes I need it. I need to check if whatever was originally in the database was altered in any way (client side). This is a textarea so they can have newlines. – Patrick Commented Nov 12, 2015 at 14:49
4 Answers
Reset to default 9You want to use the JavaScriptStringEncode mand to encode the string in a javascript patible way.
var initVal = '@HttpUtility.JavaScriptStringEncode(Model.ReferralHistoryDetail[1].ReferralComments)';
If you replace \n
character by \
,
you will have a valid syntax for multiple lines string.
So that should work
var initVal = '@Model.ReferralHistoryDetail[1].ReferralComments.Replace("\n","\\")';
I found other ways to allow multiple line string in javascript, in this answer.
You need to create an extension method that replaces the new line character with
tags.
Or use the method HtmlEncode like this:
var initVal = @Html.Raw(HttpUtility.HtmlEncode(@Model.ReferralHistoryDetail[1].ReferralComments).Replace("\n", "<br />"))
You cannot access Model properties from JavaScript like that, JavaScript cannot do anything with your model.
Create a variable up the page
@{ var value = @Model.ReferralHistoryDetail[1].ReferralComments;
Then access value in javascript, though not sure why you don't just render directly on page why do you need javascript unless I am missing something
本文标签: aspnet mvcJavascript var with a newline using MVC Model propertyStack Overflow
版权声明:本文标题:asp.net mvc - Javascript var with a newline using MVC Model property - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744854298a2628682.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论