4

I want to set a string in Javascript with an ASP.NET Html.Partial View. The problem is that Html.Partial gives an HtmlString and not a Javascript string which i can handle for example with JQuery.

Javascript code:

myfunction= function () {
   ...
   var [email protected]("_UserBadge",User.Identity.Name).ToString();
   ....
   $("#myNode").append(badge);
};

Html-Partial "_UserBadge.cshtml":

@model WT.Models.ttUser

<div style="..">
...some more lines html...
</div>

My problem is that ' at beginning and '+ at the end of each line isn't added. How can i resolve the problem?

The code results to:

var badge=
<div style="..">
...some more lines html...
</div>;

instead of a javascript string:

var badge=
'<div style="..">   '+
'...some more lines html...   '+
'</div>  ';
6
  • and the code for the Partial? Commented Jan 24, 2013 at 16:54
  • 2
    and redacted to the point of uselessness. Not posting the entire code is tantamount to asking us to guess what's wrong. Commented Jan 24, 2013 at 16:59
  • the other lines of code doesn't concern the problem i have. I think the problem should be understandable now? Commented Jan 24, 2013 at 17:05
  • no, that is not the case. I think you don't understand the problem. Commented Jan 24, 2013 at 17:11
  • +1 I think there is sufficient information now to "divine" the intent. Commented Jan 24, 2013 at 17:40

2 Answers 2

2

Couldn't you just use something like:

var badge='@Html.Partial("_UserBadge",User.Identity.Name).Replace( "\n", "\\n" ).Replace( "'", "\\'" )';

You suggest in your question that you want the result to be:

'<div style="..">   '+
'...some more lines html...   '+
'</div>  ';

but note that this is equivalent to:

'<div style="..">   ...some more lines html...   </div>  ';

which might not be what you actually want (since this is different from the _UserBadge page).

What you probably actually want is something more like this:

'<div style="..">\n...some more lines html...\n</div>';

The example replaces newline characters with "\n" which will be interpreted by javascript as a newline character. Finally, you'll need to replace single quotes with "\'" to ensure that you don't accidentally terminate the string before you meant to.

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

2 Comments

@zoidbergi - Updated the answer with more explanation.
yes i understand it. thanks. i followed Corneliu because i had still problems with your code.
1

I think you could use Json.NET for serializing objects to json (it will escape all the string characters).

Something like:

@using Newtonsoft.Json

myfunction= function () {
   ...
   var badge=@(Html.Raw(JsonConvert.SerializeObject(Html.Partial("_UserBadge",User.Identity.Name))));
   ....
   $("#myNode").append(badge);
};

3 Comments

the line breaks are gone now. Any idea why the string is html encoded now? I try @(HttpUtility.HtmlDecode(JsonConvert.SerializeObject(@Html.Partial("_UserBadge", Utils.getUser(User.Identity.Name)).ToString()))) but the string is still encoded
This is(nearly) the solution! There are still quotation marks " at beginning and end but i think these can be removed with some javascript code like ´substring()´
badge.substr(1).slice(0, -1); does this

Your Answer

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.