0

我在 ASP.Net Razer 视图中使用引号时遇到了一些困难,因为我一直将它们替换为 '

例如,如果我键入:

@{string test = "String 'with' quotes"}
@test

我最终得到:

String 'with' quotes

在上面的示例中,这是可取的。

但是,以第二个例子为例:

@{string myJSFunction = myJSFunction('myString')"}
@myJSFunction 

我最终得到:

myJSFunction('myString')

这会导致 javascript 函数损坏。

我曾尝试用 来排除引号\,但它们似乎没有任何效果。

这是我面临的实际问题:

@using (Ajax.BeginForm(
  "_MonthRanges", 
  "Projects", 
  new { id = ViewBag.InputResourceID }, 
  new AjaxOptions { 
      HttpMethod = "POST", 
      UpdateTargetId = "MonthRanges", 
      InsertionMode = InsertionMode.Replace,
      OnComplete = @myJSFunction
}))

有什么建议吗?

4

3 回答 3

2

在 razor 中,@将编码和渲染。试试Html.Raw方法。此方法返回非 HTML 编码的标记。

@Html.Raw(myJSFunction)

所以如果你想在脚本块中执行函数,你可以这样做

@{string myJSFunction = "alert('Rise and Shine')";}
<script type="text/javascript">  
    @Html.Raw(myJSFunction)
</script>
于 2013-04-11T13:53:26.957 回答
2

运算符始终对@字符串进行 HTML 编码。如果您想要原始字符串值,请使用@Html.Raw(myJSFunction).

于 2013-04-11T13:54:08.587 回答
1

已经提到了您的问题的解决方案(Html.Raw()),但我不确定您为什么在给出的代码示例中使用 @。从外观上看,当您尝试分配它时,您已经在代码块中,那么为什么要使用剃须刀。

@using (Ajax.BeginForm(
  "_MonthRanges", 
  "Projects", 
  new { id = ViewBag.InputResourceID }, 
  new AjaxOptions { 
      HttpMethod = "POST", 
      UpdateTargetId = "MonthRanges", 
      InsertionMode = InsertionMode.Replace,
      OnComplete = @myJSFunction
}))

应该

@using (Ajax.BeginForm(
  "_MonthRanges", 
  "Projects", 
  new { id = ViewBag.InputResourceID }, 
  new AjaxOptions { 
      HttpMethod = "POST", 
      UpdateTargetId = "MonthRanges", 
      InsertionMode = InsertionMode.Replace,
      OnComplete = myJSFunction
}))

@ 告诉页面的 StreamWriter 将结果写入响应,因此旨在在 HtmlEncoding 之后注入页面。使用 Html.Raw 绕过了 Html 编码方面,但在您的情况下,您不是直接自己编写页面,而是让Html.BeginForm处理它,因此您不需要在OnComplete函数分配中使用 @。

更新:好的,我忘了提到的另一件事是传递给 AjaxOptions 的方法只是 Javascript 函数,看起来没有参数。参数由处理所有接线的 Unobtrusive 脚本自动传递。所以你的表格应该是这样的......

@using (Ajax.BeginForm(
  "_MonthRanges", 
  "Projects", 
  new { id = ViewBag.InputResourceID }, 
  new AjaxOptions { 
      HttpMethod = "POST", 
      UpdateTargetId = "MonthRanges", 
      InsertionMode = InsertionMode.Replace,
      OnComplete = "myJSFunction"
}))

它会处理剩下的事情。如果您需要在myJSFunction函数中传递其他参数,您可能必须通过其他方式公开它们。可以作为 javascript 变量,也可以将其与其他元素关联并使用$("elementselector").data("dataAttributeName")(这是我首选和建议的方法)访问它。对不显眼的 ajax JavaScript 库进行的常见修改之一(我希望 Microsoft 能够采用此更改并完成它)是为this触发 ajax 事件的元素设置上下文。因此,在这种情况下,它将this等于表单元素。考虑到这一切,这是我的建议。

首先: 修改您的 Unobtrusive Ajax 脚本文件,以便将源元素分配给this关键字,这样它就可以在您的 JavaScript 中处理不显眼的事件。 这个问题概述了它所涉及的内容(添加到文件中的一行)

第二: 将您的字符串添加到表单上的数据属性之一

@using (Ajax.BeginForm(
    "_MonthRanges",
    "Projects",
    new {id = ViewBag.InputResourceID},
    new AjaxOptions
        {
            HttpMethod = "POST",
            UpdateTargetId = "MonthRanges",
            InsertionMode = InsertionMode.Replace,

            OnComplete = "myJSFunction"
        },
    new { data_parameter_name = "myString" }))

第三: 从处理您的事件的 javascriptfunction 内部访问您的参数。

<script>
    function myJSFunction(data, textStatus, jqXHR)
    {
        //data, textStatus and jqXHR are set for you by the unobtrusive ajax script file automatically, feel free to use them
        var parameter = $(this).data("parameterName");
        alert(parameter);
    }
</script>
于 2013-04-11T14:30:35.777 回答