开发者

Pass a string from the controller to a partial view as javascript function parameter

开发者 https://www.devze.com 2023-03-24 19:41 出处:网络
I have a controller public ActionResult controller(){ Viewclass view = new Viewclass(); ... return PartialView(\"Tab\", view);

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=开发者_如何学C"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?


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.


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消