2

Controller

 IEnumerable<AvgPosGAFields> _myList = helper.ConvertToListAvgPosGa(locDataSetGACampaigns.Tables[0]);        
 ViewData["hourlydata"] = _myList;

i want to use pass this ViewData to my partial to fill a table i am using renderpartial to render my partial my. How can i pass this ViewData ? and how can i use foreach on it??

Main View:

 Html.RenderPartial( "HourlyDetails",new ViewDataDictionary { { "hourlydata", 0 } } );

Partial View contains a table which is to be filled by the model in ViewData

hutchonoid
  • 32,982
  • 15
  • 99
  • 104
rao
  • 223
  • 2
  • 7
  • 17

4 Answers4

2

As an alternative I would recommend switching ViewData to use View Models as they're strongly typed instead.

You could do this in the following way:

Create a View Model

public class AViewModel
{
  public IEnumerable<AvgPosGAFields> HourlyData { get; set; }
}

Controller

var model = new AViewModel();
model.HourlyData = helper.ConvertToListAvgPosGa(locDataSetGACampaigns.Tables[0]);

return View(model);

The above assumes your method returns an IEnumerable<AvgPosGAFields>.

View

Add a model reference at the top an then pass the model into your partial as follows:

@model AViewModel
...
@Html.Partial("HourlyDetails", Model.HourlyData)

Partial View

Also add the model reference to the top of your partial view i.e.

@model IEnumerable<AvgPosGAFields>

This means you will be able to loop your model in the partial as follows:

@foreach(var avgPosGAField in Model)
{
      @avgPosGAField.FooProperty
}
hutchonoid
  • 32,982
  • 15
  • 99
  • 104
  • 1
    Yes. Passing elements of a collection to partial views is a strong sign that it is time to use a view model with strongly typed partials. – Jakotheshadows Feb 20 '14 at 09:40
0

You can use Html.Action() in controller:

public ActionResult RenderAction()
{
   //....
   IEnumerable<AvgPosGAFields> _myList = helper.ConvertToListAvgPosGa(locDataSetGACampaigns.Tables[0]);        

   return PartialView( _myList );
}

And call it from other view you want:

@Html.Action("RenderAction", "Controller")

And in RenderAction view, use foreach loop.

Jeyhun Rahimov
  • 3,769
  • 6
  • 47
  • 90
0

Check if you somehow accidently or partly on purpose overwrite the full ViewData, my problem was overwriting the ViewData for templating, without realizing the rest of the ViewData was gone.

@Html.Partial("_EmployeeFormContent", @Model.Employee, new ViewDataDictionary() { TemplateInfo = new TemplateInfo { HtmlFieldPrefix = nameof(Model.Employee) } })

Adding the current ViewData to the new ViewDataDictionary fixed it:

    @Html.Partial("_EmployeeFormContent", @Model.Employee, new ViewDataDictionary(ViewData) { TemplateInfo = new TemplateInfo { HtmlFieldPrefix = nameof(Model.Employee) } })
Nick N.
  • 12,902
  • 7
  • 57
  • 75
0

use a viewbag instead of viewdata

IEnumerable<AvgPosGAFields> _myList = helper.ConvertToListAvgPosGa(locDataSetGACampaigns.Tables[0]);
ViewData["hourlydata"] = _myList;

so try

ViewBag.hourlydata = _myList;

then even if it does not work, try this solution
http://www.codenoevil.com/pass-additional-viewdata-asp-net-mvc-4/