简体   繁体   English

具有不同模型的多个视图中的 MVC PartialView

[英]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 .我想要一个包含搜索表单和搜索结果并使用 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?我是否可以这样做,或者我是否必须在每个视图中创建搜索表单或为每个视图创建一个局部视图,以便视图和局部视图使用相同的 model?

If I use the Suppliers model for the partial view and another model for the view I just get error:如果我将Suppliers model 用于局部视图,将另一个 model 用于视图,我只会得到错误:

The model item passed into the dictionary is of type 'x', but this dictionary requires a model item of type 'y'.传入字典的 model 项的类型为“x”,但该字典需要一个“y”类型的 model 项。

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.您可以从您拥有的“共享”视图中删除 Model,并让它与您从控制器传递给视图的 ViewBag 或 ViewData 一起使用。 Obviously you will need to populate this view bag or view data within all the controller actions that will return this shared partial view.显然,您需要在将返回此共享部分视图的所有 controller 操作中填充此视图包或视图数据。

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:除了将 Suppliers 作为共享视图的视图 model 之外,您还可以在视图 model “Supplier”中拥有另一个属性,您可以使用它,但是当您渲染共享视图时,您需要指定属性并将其作为 ZA5785068706485EEE9C 传递到共享视图,例如:

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.现在,您必须对呈现此共享的所有其他视图执行相同的操作,并且基本上将此“SharedViewModel”作为这些视图模型中的属性,并将Model.SharedViewModel传递给共享视图。

For sure there are other options too that you can find out once you get more comfortable with Shared Views in MVC.当然,一旦您对 MVC 中的共享视图更加熟悉,您还可以找到其他选项。

So each partial view is going to be directly tied to a model.所以每个局部视图都将直接绑定到 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)确保您考虑到您所说的如何使用 model(即@model [model name here]@model IEnumerable<[model name here]>如果您正在枚举您的结果,例如可能显示的结果来自数据库的搜索结果列表;尝试使用单独的视图模型。这将允许您单独更改与特定表/列交互的方式(即,如果您正在枚举它们)

First of all you need to create partial view under shared folder with name _SearchBox.schtml.首先,您需要在名称为_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.否则,您可以在 controller 中创建操作,如果您希望从 DB 获取供应并且您的父视图中没有供应列表。

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

and call it from view:并从视图中调用它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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