简体   繁体   English

MVC部分视图以显示列表

[英]MVC Partial view to Display list

I have an Action result that returns a list 我有一个操作结果返回一个列表

public ActionResult GetData(Profiles profiles)
{
    Vertical Vdata = new Vertical();
    List<Ver> Vertical = new List<Ver>();
       //Code to fill list
    return View(Vertical);
}

And then a partial view to display the List 然后局部显示列表

@model  IEnumerable< List<VerticalContainer.Models.Vertical>>

@foreach(var item in Model)
{
 <span>@item.name</span>
}

I'm not sure how to render the partial view from my main view 我不确定如何从主视图渲染部分视图

@Html.Action("GetData")?? 

What do I pass with the Html.Action? Html.Action通过什么? or Should I use Partial/RenderPartial? 还是我应该使用Partial / RenderPartial?

1- the correct way to render actions is as per the following: 1-正确的动作呈现方式如下:

@Html.RenderAction("ACTION_NAME","CONTROLLER_NAME")

where you replace both action name and controller name with the correct values according to your solution. 根据您的解决方案,使用正确的值替换操作名称和控制器名称。

2- For the passed model, if this action is being rendered inside a view that has a Model property of Profiles , then you don't have to specify the model to be passed to the action, as it will implicitly read it from the parent view. 2-对于传递的模型,如果在具有ProfilesModel属性的视图中渲染此动作,则不必指定要传递给该动作的模型,因为它将隐式地从父级读取模型视图。

if this is not the case, then you will need to store your Profiles values inside a medium variable (for example inside a ViewBag property) and then you will pass it when calling the action, so it should work on this passed model istead of working on the parent view one. 如果不是这种情况,则需要将Profiles值存储在一个中等变量中(例如,在ViewBag属性中),然后在调用该操作时将其传递,因此它应在此传递的模型上起作用而不是起作用在父视图上。

example: @Html.RenderAction("ACTION_NAME","CONTROLLER_NAME", YOUR_MODEL_VALUE_HERE) 例如: @Html.RenderAction("ACTION_NAME","CONTROLLER_NAME", YOUR_MODEL_VALUE_HERE)

Suggestion : if this parital view will just render the list passed, you can skip creating the mentioned action, and just make a call which can render the partial view, passing to it the correct list so it can work with. 建议 :如果此局部视图仅呈现已传递的列表,则可以跳过创建提到的操作的过程,而只需进行调用即可呈现部分视图,并将正确的列表传递给该局部视图以便可以使用。

To make it more clear, using your question I can see that the action named GetData is just constructing the list called Vertical from the Profiles model passed, so you can construct this list in your original action ( Index , Details , WHAT_EVER_THE_NAME_IS ) and store it in a ViewBag , then you can call RenderParial instead of RenderAction and this should result in the same output as your mentioned scenario. 为了更加清楚,使用您的问题,我可以看到名为GetData的操作只是从传递的Profiles模型中构造了名为Vertical的列表,因此您可以在原始操作( IndexDetailsWHAT_EVER_THE_NAME_IS )中构造此列表并将其存储在ViewBag ,则可以调用RenderParial而不是RenderAction ,这将导致与您提到的方案相同的输出。

example: @{ Html.Partial("ViewName", YOUR_MODEL_HERE);}
@{Html.RenderAction("actionName","controllerName");}

From your main view, you may simply render your Partial view as: 从主视图中,您可以简单地将“ 部分视图”呈现为:

 @Html.Partial("_PartialViewName", VerticalList);

VerticalList in this case will be the list of type VerticalContainer.Models.Vertical , that you populated in controller action. 在这种情况下, VerticalList将是您在控制器操作中填充的VerticalContainer.Models.Vertical类型的列表。

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

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