rao
rao

Reputation: 223

Passing ViewData to Partial View

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

Upvotes: 2

Views: 16274

Answers (4)

Otacilio Compassi
Otacilio Compassi

Reputation: 1

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/

Upvotes: 0

Nick N.
Nick N.

Reputation: 13559

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

Upvotes: 0

hutchonoid
hutchonoid

Reputation: 33306

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
}

Upvotes: 2

Jeyhun Rahimov
Jeyhun Rahimov

Reputation: 3787

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.

Upvotes: 0

Related Questions