16

I have a partial view in which I am trying to get the values from the parent view. This is what I am trying:

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

And this is the partial view:

      <!-- 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> 

Is it possible to get the value straight away? I don't have any model or controller assigned to the partial view. Thx in advance, Laziale

Updated Code:

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

You think that will work?

Laziale
  • 7,965
  • 46
  • 146
  • 262

2 Answers2

24

Sorry i don't understan your question quite well. You can get ViewData values in partial like this:

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

You can also create new ViewDataDictionary extending current view ViewDataDictionary like this:

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

it will work like this:

 @{
     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> 
karaxuna
  • 26,752
  • 13
  • 82
  • 117
  • is there a way to get that as ViewData[0] for example? Because those two values might be different every time I'll call that partial view.. – Laziale Dec 04 '12 at 18:54
  • ViewData.Values.FirstOrDefault() this will get first value – karaxuna Dec 04 '12 at 19:01
  • But it's not nice solution to get first value from ViewData – karaxuna Dec 04 '12 at 19:02
  • @Html.Partial("Shared", "Home", new ViewDataDictionary(ViewData) { { "MyVariable", "8sd8sds8das8d" } }) and pass different values to MyVariable, then you will get it in view like this: ViewData["MyVariable"] – karaxuna Dec 04 '12 at 19:04
  • in my case I want to send two values 9595959 and 8sd8sds8das8d. – Laziale Dec 04 '12 at 19:05
  • then write @Html.Partial("Shared", "Home", new ViewDataDictionary(ViewData) { { "First", "9595959" }, { "Second", "8sd8sds8das8d" } }) – karaxuna Dec 04 '12 at 19:06
  • var first = ViewData["First"]; var second = ViewData["Second"]; – karaxuna Dec 04 '12 at 19:07
  • take a look at razor syntax: http://www.asp.net/web-pages/tutorials/basics/2-introduction-to-asp-net-web-programming-using-the-razor-syntax – karaxuna Dec 04 '12 at 19:14
  • I am getting blank value: http://gyazo.com/b258d806ef2e41278d747b8b5ba0c16c.png?1354650123 – Laziale Dec 04 '12 at 19:42
  • You are doing something wrong. I think you should learn more first about web. – karaxuna Dec 04 '12 at 19:45
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/20585/discussion-between-laziale-and-karaxuna) – Laziale Dec 04 '12 at 19:52
0

link

Here is an example of someone doing the same thing I think you're wanting to do.

Also, within your partial, just do ViewData["9595959"] to hook into that specific data.

Community
  • 1
  • 1
IyaTaisho
  • 863
  • 19
  • 42