How to render partial view ?

Hello All,

I want to have common dropdownlist to change the theme; So, that I have created a partial view for the same.

However, my question is how can I render a partial view with data in Layout page ??

I have called and rendered like below from layout;

@Html.Action("GetThemeList","ThemeController")

and then in method rendered a partial view;

 List<SelectListItem> li = new List<SelectListItem>();
            li.Add(new SelectListItem { Text = "DarkRed", Value = "DarkRed" });
            li.Add(new SelectListItem { Text = "DarkBlue", Value = "DarkBlue" });

            ViewData["Themes"] = li;

            return PartialView("_ThemeChangerPartial");

Is this a correct way ? as my controller method called every time because of @html.action ? Can I conditionally call this method ?

You are doing it right what i think, in what way you want to improve it?

ehsansajjad465

You are doing it right what i think, in what way you want to improve it?

Improvement means as it will be called whenever any view is rendered. How can I add the condition that it is (dropdown list, partial view) already present OR like that ??

Also, how can I bind the model to drop down list in view ?

Leave a Reply