Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将引号(";或';)和引号字符串写入Razor视图_C#_Razor_Asp.net Mvc 5 - Fatal编程技术网

C# 如何将引号(";或';)和引号字符串写入Razor视图

C# 如何将引号(";或';)和引号字符串写入Razor视图,c#,razor,asp.net-mvc-5,C#,Razor,Asp.net Mvc 5,我在RESX文件中保存了一系列要在JS中使用的常量,例如: DATE_PICKER_FORMAT yyyy-mm-dd DATETIME_FORMAT yyyy-mm-dd hh:mm:ss MONTH_PICKER_FORMAT yyyy-mm 我编写了一个简单的类来帮助在Razor视图中将其写入JS: public static class JavascriptResourceRenderer { private static string Render

我在RESX文件中保存了一系列要在JS中使用的常量,例如:

DATE_PICKER_FORMAT   yyyy-mm-dd  
DATETIME_FORMAT      yyyy-mm-dd hh:mm:ss  
MONTH_PICKER_FORMAT  yyyy-mm  
我编写了一个简单的类来帮助在Razor视图中将其写入JS:

public static class JavascriptResourceRenderer
{
    private static string Render(ResourceSet resources)
    {
        string resourceString = "";

        foreach (DictionaryEntry resource in resources)
        {
            resourceString += String.Format("var {0} = '{1}'; ", resource.Key, resource.Value);
        }

        return resourceString;
    }

    public static string RenderPageConstants()
    {
        ResourceSet resources = PageConstants.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
        return Render(resources);
    }
}
在我看来,我正在这样做:

@section Scripts
{
    <script>
        @JavascriptResourceRenderer.RenderPageConstants()
    </script>
}
我怎么写

<script>
    var MONTH_PICKER_FORMAT = "yyyy-mm"; // or 'yyyy-mm' (I want the quotes!)
</script>

var MONTH\u PICKER\u FORMAT=“yyyy-mm”//或者“yyyy-mm”(我想要引号!)

查看?

您的字符串正在进行HTML编码

您需要输出原始文本。您的视图需要执行以下操作:

@section Scripts
{
    <script>
        @Html.Raw(JavascriptResourceRenderer.RenderPageConstants())
    </script>
}
@节脚本
{
@Html.Raw(JavascriptResourceRenderer.RenderPageConstants())
}

您应该将输出返回为,否则MVC将对其进行编码:

private static MvcHtmlString Render(ResourceSet resources)
{
    string resourceString = "";

    foreach (DictionaryEntry resource in resources)
    {
        resourceString += String.Format("var {0} = '{1}'; ", resource.Key, resource.Value);
    }

    return new MvcHtmlString(resourceString);
}
或者,您可以在视图中使用Html Helper方法,但每次调用该方法时都需要记住这样做(这就是为什么我不建议这样做的原因):


谢谢DavidG有更完整的答案,但很高兴了解Html.Raw!
@section Scripts
{
    <script>
        @Html.Raw(JavascriptResourceRenderer.RenderPageConstants())
    </script>
}
private static MvcHtmlString Render(ResourceSet resources)
{
    string resourceString = "";

    foreach (DictionaryEntry resource in resources)
    {
        resourceString += String.Format("var {0} = '{1}'; ", resource.Key, resource.Value);
    }

    return new MvcHtmlString(resourceString);
}
@Html.Raw(JavascriptResourceRenderer.RenderPageConstants())