简体   繁体   中英

ASP.NET - Getting Data from my Model into a JavaScript variable and populating a textarea with said variable

This may seem like a simple question and I haven't been able to find an easy answer to this question. I have a Model (a view model with various pieces of data) that I'm loading into JavaScript variables as the page loads.

An example of this is:

var UserDescription = '@Model.userProfile.UserDescription';

It's working okay and the UserDescription does indeed have the text that I want it to have with one exception, I'm getting the special characters as EDIT: these characters are HTML codes apparently( http://www.w3.org/MarkUp/html-spec/html-spec_13.html ). An example is if my UserDescription contains the word 'You've' the value in UserDescription will contain 'you' followed by '&#39' followed by 've'

I did an alert and indeed the value held in UserDescription does actually contain the character string regardless of putting the data in the textarea. Just in case I'm doing something wrong here's some more of the code:

The textarea:

<textarea type="text" class="form-control" id="UserDescription">
                </textarea>

The jquery to populate said textarea

$("#UserDescription").val(UserDescription);

And from my viewmodel(I have no attributes on it):

public string Description{ get; set; }

I have also looked in the database and in the database the value is correct. This seems like something that should be easy to resolve and maybe I've just been looking at code too long today but any help would be greatly appreciated.

您可能要尝试以下操作:

$("#UserDescription").val($("<span></span>").html(UserDescription).text());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM