0

I tried to make a partial view of a dropdown list to load data using ViewModel as context.categories.tolist(). and then Render the partial view on the layout page! but render page giving an error! asking the same ViewModel to pass! Have any idea how to design a dropdownlist on the layout page without using @model ViewModel !!?

public ActionResult CategoryDropDown()
        {
            HomeViewModel model = new HomeViewModel();

            model.AllCategory = CategoriesService.Instance.allCategory();
          //getting category list from database

            return PartialView(model);
        }

Views/Shared/_Layout.cshtml

<!DOCTYPE html>
<html>
<header>
all the headers link///
</header>
<section>
@if (Model != null && Model.AllCategory.Count > 0)
{
    <div class="col-lg-3">
        <div class="hero__categories">
            <div class="hero__categories__all">
                <i class="fa fa-bars"></i>
                <span>All departments</span>
            </div>
            <ul>
                @foreach (var category in Model.AllCategory)
                {
                    <li><a href="#">@category.Name</a></li>
                }


            </ul>
        </div>
    </div>
}
</section>
Renderbody()


   
    @RenderSection("scripts", required: false)

</html>
~~
  • What's the error? What model is needed by the view? what model you are passing? Can you share the code of view and controller action? How do you set `context.categories`? What is `context` here? – Chetan Dec 03 '21 at 03:46
  • I know that the layout code is wrong !! my question is how can I get the category as a dropdown list in layout page using @Render partial or Action!! ,so that layout code looks clean! – Md. Yeahia Khan Dec 03 '21 at 08:22
  • Try converting the `Model` object to `HomeViewModel` object in Layout. `HomeViewModel modelObj = Model as HomeViewModel;` and then use `modelObj.AllCategory` to create dropdown list. – Chetan Dec 03 '21 at 08:33
  • is there any other way? without passing the HomeViewModel directly to Layout, just as render or partial render or section render !!? – Md. Yeahia Khan Dec 03 '21 at 09:37
  • ` CategoriesService.Instance.allCategory();` call this directly in Layout page, iterate thru the collection to create the dropdownlist. – Chetan Dec 03 '21 at 09:38
  • I am trying do it in other view and render in layout page .. will it be possible ? – Md. Yeahia Khan Dec 03 '21 at 09:47
  • LayOut page will be used in my pages... it will be repeated code if you write this code in the other view. So if you want to pass model from controller then you will have to set the model from all the controller action which calls view with this Layout. Else you just write this code only once in Layout. – Chetan Dec 03 '21 at 09:50
  • https://stackoverflow.com/questions/37306604/how-to-design-an-mvc5-global-search-feature-in-layout-cshtml – Md. Yeahia Khan Dec 03 '21 at 09:53
  • Does it solve your problem? – Chetan Dec 03 '21 at 09:55
  • it does not working for dropdown list ! – Md. Yeahia Khan Dec 03 '21 at 10:04
  • oh !! yes its working !! I did a slight mistake . – Md. Yeahia Khan Dec 03 '21 at 10:10

0 Answers0