1

I have a controller

public ActionResult controller(){
Viewclass view = new Viewclass();
... 
return PartialView("Tab", view);
}

In which the view class is something like, the string A and B is my compiled JSON string.

public class ViewClass
{public string A {get;set;}
public string B {get;set;}
}

In the Partial view, I want to do

<script type="text/javascript">
RunMap( @Model.A, @Model.B);
</script>

The problem is the " is parsed as &quot, like {&quotCity&quot:} into the function. How can I fix this?

2 Answers 2

3

Use @Html.Raw to treat the string as raw HTML (e.g MVC will not encode it)

<script type="text/javascript">
   RunMap(@Html.Raw(Model.A), @Html.Raw(Model.B));
</script>

Of course, you need to make sure that the input (e.g the data) it handled carefully.

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

Comments

1

This should work: RunMap( "@Model.A", "@Model.B");

If you want to pass them as JSON objects, then take out the quotes.

3 Comments

I got something like {&quot;Cities&quot;:[{&q....64635201372,&quot;Cities&quot;:0}}) in the result
How are you creating the JSON string? Maybe you can use JsonValueProviderFactory and declare your props as Json. Here's a related article: haacked.com/archive/2010/04/15/…
I used a StringBuilder and create my own (since I need to add on some logics and please see my ViewClass). How can I correctly declare the return type to avoid this problem?

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.