15

我有一个部分视图,我试图从父视图中获取值。这就是我正在尝试的:

 @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> 
}

你认为这会奏效吗?

4

2 回答 2

23

对不起,我不太明白你的问题。您可以像这样获取部分 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> 
于 2012-12-04T18:47:26.410 回答
0

关联

这是一个人在做我认为你想做的事情的例子。

此外,在您的部分中,只需执行 ViewData["9595959"] 即可挂钩该特定数据。

于 2012-12-04T18:48:24.043 回答