1

I'm new to ASP.NET MVC and I have a partial view with a combo box, which I need to dynamically populate.

My understanding is that I can put the list data in the ViewBag for the view, and it will be available from my partial view.

However, I want to be able to use the partial view from several different pages. Must all pages that use the partial view populate ViewBag with the list data? Is there any way to encapsulate this within my partial view?

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466

3 Answers3

1

You should pass data to your partial View using

    @Html.Partial("", yourData)

and access them in your partial view using

    @ViewData.Model
dknaack
  • 60,192
  • 27
  • 155
  • 202
  • But where would I do this from? From every pages that uses the partial view? – Jonathan Wood Oct 24 '11 at 18:01
  • Yes. Off course you can define "defaults" if ViewData.Model is null. But, using my answer, is the right way. – dknaack Oct 24 '11 at 18:03
  • Thanks. This is a list of items and there can be no defaults. But I'm troubled by the fact that I can't encapsulate this logic within the partial view. If I use it from a dozen views, then I need to add code to provide the list in a dozen places. – Jonathan Wood Oct 24 '11 at 18:05
  • If you have always the same data, consider using a static class that holds your data. You can access them from your partial without passing it in. Its not really recommend to use the ViewBag for that. ViewBag is dynamic. – dknaack Oct 24 '11 at 18:07
  • The data will come from a database and will not always be the same. However, I may be able to contain it within a static class here. Thanks. The structure here bothers me a little bit though. – Jonathan Wood Oct 24 '11 at 18:09
  • If the question is, passing data to a partial view the right way, then my answer is right. In your case create a static class with static method that gets your data from the database. Static method doesnt mean the data is static. – dknaack Oct 24 '11 at 18:11
1

Look at this question: ViewModel Best Practices. May be you should create a base view model containing the list data and (if needed) inherit other view models from it.

For instance:

    class ViewModelWithFooList
    {
        public List<Foo> FooList
        {
            get { return new List<Foo>() { new Foo("one"), new Foo("two"), new Foo("three") }; }
        }
    }

You can inherit other view models from ViewModelWithFooList if you need to.

In controller:

    public ActionResult GetList()
    {
        return View(new ViewModelWithFooList());
    }

And in your views:

    @Html.RenderPartial("FooList", Model.FooList);

Views and partial view "FooList" must be strongly typed.

Community
  • 1
  • 1
Maksim Tyutmanov
  • 423
  • 1
  • 5
  • 12
  • I've never used it, but I think you may also try [this](http://msdn.microsoft.com/en-us/library/system.web.mvc.html.childactionextensions.renderaction.aspx). – Maksim Tyutmanov Oct 24 '11 at 18:34
1

You can use a child action for this.

Make an action in a controller that just sets the ViewBag and returns the combo box partial view.

e.g.

 [ChildActionOnly]
 public ActionResult MyDropDown()
 {
     ViewBag.DropDown = blah;
     return PartialView();
 }

Then, any view that needs to use that dropdown list renders it as a child action.

e.g.

@{ Html.RenderAction("MyDropDown", "MyController"); }
StanK
  • 4,750
  • 2
  • 22
  • 46