mvchtmlstring html编码,没有Razor编码的MvcHtmlString

I'm writing a method that generating a javascript code and sending the razor view.

But razor view encoding this according to its head.Codes is below.

This is my method body, returning a mvchtmlstring:

StringBuilder scriptHtml = new StringBuilder();

scriptHtml.Append("");

scriptHtml.Append(@"var {0} = {{

id: 'someText',

title: '{1}',

$(document).ready(function () {{

$.function({0});

}});");

scriptHtml.Append("");

var output = string.Format(scriptHtml.ToString(), this.Id, this.Title);

return MvcHtmlString.Create(output);

So now, its output is :

@<script>var tlyPageGuide,,"direction":"Right"

Something like this growing with encoded.

That's what I want is the Razor should not change my code while cshtml is creating and I gotta get pure javascript code with rendered.

Note : I don't want to use Html.Raw() for the solution.I want to make this on server-side.

Talk1:

what do you mean "on server side"? All your Razor Views rendered on server side.

Talk2:

I mean, I don't want any additional Html.Raw() code in the view. I want to encode it on return MvcHtmlString.Create(output) or method body for example. By the way; Html.Raw() is working but I don't want to use it every time and every where when I'm invoking my method via Helper like this @Html.Raw(Html.MyHelper.MyMethod()).

Solutions1

Have you considered using partial view?

For instance: (this code may contain errors but you get the idea):

class MyScriptModel

{

public string Id {get;set;}

public string Title {get;set;}

}

//your partial view: my_partial.cshtml

@model MyScriptModel

//extracted from your string builder

var @Model.Id = {{

id: 'someText',

title: '@Model.Title',

$(document).ready(function () {{

$.function(@Model.Id);

}});

//instantiate the model

var model = new MyScriptModel {Id="someid", Title="some title"};

//using your partial view

@Html.Partial("my_partial", model)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值