MVC PartialView in multiple Views with different models

后端 未结 4 1923
南方客
南方客 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:44

    So each partial view is going to be directly tied to a model. If you're looking to have different information from different models in the same view, your best bet would be the concept of a modal. In terms of having search and search result partial views rendering together on the same view. Make sure you are accounting for where you are saying how you are using the model (i.e. @model [model name here] or @model IEnumberable<[model name here]> IF you are enumerating over your results like what would be likely for displaying a list of search results from a database; try using the a separate viewmodel. This will allow you to change how you are interacting with specific tables/columns indiviudally (i.e. if you are enumerating through them)

    0 讨论(0)
  • 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<your.project.Supplies>
    <div id="search-box">
     @using (Html.BeginForm("MakeSearch", "SearchController", FormMethod.Get))
         {
        <input type="search" name="search" />
        <input type="submit"  value="GO" />
    }
    </div>
    

    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"); }
    
    0 讨论(0)
  • 2021-01-27 03:46

    Try using a partial action instead of partial view.

    http://pratapreddypilaka.blogspot.co.uk/2011/11/htmlpartial-vs-htmlaction-mvc-razor.html?m=1

    0 讨论(0)
  • 2021-01-27 03:55

    You can definitely do that with different implementations.

    First Option:

    You can drop the Model from the "Shared" view that you have and have it work with a ViewBag or ViewData that you pass to the view from your controllers. Obviously you will need to populate this view bag or view data within all the controller actions that will return this shared partial view.

    Second Options:

    Instead of having Suppliers as the view model of the shared view, you can have another property in the view model "Supplier" that you can use, but when you are rendering the shared view you need to specify the property and pass it as the Model to the shared view like:

    Html.RenderPartial("MySharedView", Model.SharedViewModel);
    

    Now you have to do the same thing for all other views that render this shared and basically have this "SharedViewModel" as a property in those view models and pass the Model.SharedViewModel to the shared view.

    For sure there are other options too that you can find out once you get more comfortable with Shared Views in MVC.

    0 讨论(0)
提交回复
热议问题