Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get viewdatadictionary value in partial view

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?

like image 968
Laziale Avatar asked Dec 04 '12 18:12

Laziale


People also ask

How to return partial View in MVC?

In ASP.NET Core MVC, a controller's ViewResult is capable of returning either a view or a partial view. In Razor Pages, a PageModel can return a partial view represented as a PartialViewResult object. Referencing and rendering partial views is described in the Reference a partial view section.

How to render partial View in Razor?

For this go to Solution Explorer then select Views -> Shared Folder -> Right-click -> Add View. Now for the View -> Home -> Index. cshtml. Here I am rendering a Partial View using 4 types, so the index.

What is partial View in Razor pages?

Partial Pages or Views are Razor files containing snippets of HTML and server-side code to be included in any number of pages or layouts. Partial pages can be used to break up complex pages into smaller units, thereby reducing the complexity and allowing teams to work on different units concurrently.


1 Answers

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> 
like image 69
karaxuna Avatar answered Sep 21 '22 02:09

karaxuna