MVC PartialView in multiple Views with different models

后端 未结 4 1989
南方客
南方客 2021-01-27 02:58

This may be a silly question and is more of a question about how to do something rather than an actual coding issue.

I want to have a partial view which contains a searc

4条回答
  •  感动是毒
    2021-01-27 03:45

    First of all you need to create partial view under shared folder with name _SearchBox.schtml. It can be something like this:

    @model IEnumerable
    
    

    Then you can call it from any view by calling

     @{Html.RenderPartial("_SearchBox", listOfSupplies);}
    

    Otherwise you can create action in controller if wish to get supplies from DB and you don't have a list of supplies on your parent view.

     [ChildActionOnlyAttribute]
            public ActionResult _SearchBox()
            {           
                var supplies= _service.GetSupplies();  
                 PartialView(supplies);            
            }
    

    and call it from view:

     @{ Html.RenderAction("_SearchBox", "Search"); }
    

提交回复
热议问题