17

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?

2 Answers 2

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> 
Sign up to request clarification or add additional context in comments.

11 Comments

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..
ViewData.Values.FirstOrDefault() this will get first value
But it's not nice solution to get first value from ViewData
@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"]
in my case I want to send two values 9595959 and 8sd8sds8das8d.
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.