c# - Add Html.Partial to Javascript - Stack Overflow
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> ';
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> ';
Share
Improve this question
edited Jan 24, 2013 at 17:43
daniel
asked Jan 24, 2013 at 16:49
danieldaniel
35.8k40 gold badges107 silver badges162 bronze badges
6
- and the code for the Partial? – jmoerdyk 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. – jmoerdyk 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? – daniel Commented Jan 24, 2013 at 17:05
- no, that is not the case. I think you don't understand the problem. – daniel Commented Jan 24, 2013 at 17:11
- +1 I think there is sufficient information now to "divine" the intent. – JDB Commented Jan 24, 2013 at 17:40
2 Answers
Reset to default 2Couldn'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.
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);
};
- 微软发布平板是明智之举还是自寻死路
- Google的AR眼镜
- 常受电脑辐射的白领男易生闺女?
- google tag manager - GTM custom template - not able to trigger callback from script - Stack Overflow
- Upgrading apache spark core from 3.3.2 to >=3.4.4 results in stackoverflowerror in logging - Stack Overflow
- why docker change value of args if put in FROM - Stack Overflow
- circom - Pedersen Commitment Homomorphic Addition Issue - Stack Overflow
- kotlin - Do I need Serialization to Transfer a MutableMap Between Minecraft Server and Client? If so, How Should I Serialize It?
- typescript - How to keep autosuggestion with generics when working with keyof? - Stack Overflow
- reactjs - How to import svg icons in Nextjs 15 - Stack Overflow
- tensorflow - Error when loading old .h5 file with latest Keras - Stack Overflow
- ms access - Execute VBScipt on local machine, triggered from remote desktop - Stack Overflow
- magento - Renovate: Get version compatibility for "external" dependencies from documentation for Docker file -
- php - How to return blob in Joomla 5 API - Stack Overflow
- javascript - Firebase Auth link - Problem with the Google login, no possibility to change to own project name - Stack Overflow
- angularjs - how to display a pdf in angular? - Stack Overflow
- tsx - Does Inversify actually require emitDecoratorMetadata for Typescript? - Stack Overflow