1

I want to render partialview inside a view with two parameters:

1) Viewdata
2) List

I am new to MVC4 Razor. Help me. I don't know how to do it. I tried this:

@{Html.RenderPartial("_SelectAllDrChemistSales", new  {"result_Type",
            ViewData["result_Type"].ToString() });}

But it is giving me error.

Dhwani
  • 7,484
  • 17
  • 78
  • 139
  • First, what the `List` got to do with any of this? Second, the `RenderPartial` helper expects you to pass a model and not an anonymous "routed values object". If `ViewData["result_Type"]` contains the model that your partial view expects, then you should be able to call `@{Html.RenderPartial("_SelectAllDrChemistSales", ViewData["result_Type"]);}`. You should provide additional information such as the error you're getting and maybe the definition of your partial view. – Andrei V Feb 12 '14 at 09:30
  • @AndreiV My partial view represent list of all Doctor Chemist Sales. So I need to pass the list. – Dhwani Feb 12 '14 at 09:32

3 Answers3

1

You need to use the Html.Partial() helper instead of Html.RenderPartial().

Try:

@Html.Partial("_SelectAllDrChemistSales", ViewData);

This will render the partial and send through the whole ViewData.

If you need to display the items in a list, for example, you can then use:

<ul>
    @for (var item in ViewData["myItems"]) // Or whatever your list is called in ViewData
    {
        <li>@item</li>
    }
</ul>

Although, like @Henk points out in his answer, you're far better off forgetting about ViewData and ViewBag as a method of getting your data into the views and look into view models. This will then give you automatic model binding, which will take 90% of the work you're going to potentially encounter if you carry on down the ViewData/ViewBag route.

Adrian Thompson Phillips
  • 6,893
  • 6
  • 38
  • 69
  • `Partial` and `RenderPartial` are the same. Except for the fact that `Partial` returns a string and needs the `@` nugget. `RenderPartial`'s return type is `void` as it renders the html directly in the output stream. See [this](http://stackoverflow.com/questions/5248183/html-partial-vs-html-renderpartial-html-action-vs-html-renderaction). – Henk Mollema Feb 12 '14 at 10:08
0

There is no such overload of the RenderPartial method where you can specify route parameters. Calling it like this should be enough:

@{ Html.RenderPartial("_SelectAllDrChemistSales"); }

You can use ViewData["result_Type"] in the partial view to get the value. If it doesn't work, you could try and use this overload where you add the ViewData object as an argument:

@{ Html.RenderPartial("_SelectAllDrChemistSales", ViewData); }


You might want to look into view models in stead of using the ViewData project.
Henk Mollema
  • 44,194
  • 12
  • 93
  • 104
0

Try like this,

  @Html.Partial("_SelectAllDrChemistSales", ViewData["result_Type"])
Jaimin
  • 7,964
  • 2
  • 25
  • 32