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

4 Answers 4

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

1 Comment

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

Comments

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

Comments

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/

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.