简体   繁体   中英

MVC PartialView in multiple Views with different models

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 search form and search results and uses model Suppliers .

This partial view would then be rendered in multiple views that use different models.

Is it possible for me to this or do I have to create the search form within each view or create a partial view for each view so that the view and partial view use the same model?

If I use the Suppliers model for the partial view and another model for the view I just get error:

The model item passed into the dictionary is of type 'x', but this dictionary requires a model item of type 'y'.

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.

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 (ie @model [model name here] or @model IEnumerable<[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 individually (ie if you are enumerating through them)

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"); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM