在局部视图中获取viewdatadictionary值

时间:2012-12-04 18:36:19

标签: asp.net-mvc-3 razor asp.net-mvc-4 partial-views

我有一个局部视图,我试图从父视图中获取值。 这就是我想要的:

 @Html.Partial("Shared", "Home", new ViewDataDictionary { { "9595959", "8sd8sds8das8d" } }) 

这是部分观点:

      <!-- Google Code for apply Conversion Page --> <script type="text/javascript">
   /* <![CDATA[ */
   var google_conversion_id = "viewdata-number1";
   var google_conversion_language = "en";
   var google_conversion_format = "2";
   var google_conversion_color = "ffffff";
   var google_conversion_label = "viewdata-number2"; var google_conversion_value = 0;
   /* ]]> */
   </script>
   <script type="text/javascript"  
   src="https://www.googleadservices.com/pagead/conversion.js">
   </script>
   <noscript>
   <div style="display:inline;">
   <img height="1" width="1" style="border-style:none;" alt=""  
   src="https://www.googleadservices.com/pagead/conversion/viewdata-number1/?value=0&amp;label=viewdata-number2&amp;guid=ON&amp;script=0"/>
   </div>
   </noscript> 

是否有可能立即获得价值?我没有为部分视图分配任何模型或控制器。 先谢谢,Laziale

更新代码:

@{
    var variable = ViewData["First"];
        <!-- Google Code for apply Conversion Page --> <script type="text/javascript">
   /* <![CDATA[ */
   var google_conversion_id = variable;
   var google_conversion_language = "en";
   var google_conversion_format = "2";
   var google_conversion_color = "ffffff";
   var google_conversion_label = "f6vICKTT6gMQzNOf3gM"; var google_conversion_value = 0;
   /* ]]> */
   </script>
   <script type="text/javascript"  
   src="https://www.googleadservices.com/pagead/conversion.js">
   </script>
   <noscript>
   <div style="display:inline;">
   <img height="1" width="1" style="border-style:none;" alt=""  
   src="https://www.googleadservices.com/pagead/conversion/1002957260/?value=0&amp;label=f6vICKTT6gMQzNOf3gM&amp;guid=ON&amp;script=0"/>
   </div>
   </noscript> 
}

您认为这会奏效吗?

2 个答案:

答案 0 :(得分:18)

对不起,我没有完全理解你的问题。您可以像这样部分获取ViewData值:

var a = (int)ViewData["9595959"]; // variable a will get value "8sd8sds8das8d"

您还可以创建新的ViewDataDictionary扩展当前视图ViewDataDictionary,如下所示:

 @Html.Partial("Shared", "Home", new ViewDataDictionary(ViewData) { { "9595959", "8sd8sds8das8d" } }) 

它会像这样工作:

 @{
     var variable = (int)ViewData["First"];
 }
    <!-- Google Code for apply Conversion Page --> <script type="text/javascript">
 /* <![CDATA[ */
 var google_conversion_id = @variable;
 var google_conversion_language = "en";
 var google_conversion_format = "2";
 var google_conversion_color = "ffffff";
 var google_conversion_label = "f6vICKTT6gMQzNOf3gM"; var google_conversion_value = 0;
 /* ]]> */
 </script>
 <script type="text/javascript" src="https://www.googleadservices.com/pagead/conversion.js">
 </script>
 <noscript>
 <div style="display:inline;">
 <img height="1" width="1" style="border-style:none;" alt=""  
 src="https://www.googleadservices.com/pagead/conversion/1002957260/?value=0&amp;label=f6vICKTT6gMQzNOf3gM&amp;guid=ON&amp;script=0"/>
 </div>
 </noscript> 

答案 1 :(得分:0)

link

以下是一个人做同样事情的例子,我认为你想要这样做。

此外,在您的部分内容中,只需将ViewData [“9595959”]挂钩到该特定数据中即可。