简体   繁体   English

MVC:如何将列表从一个视图传递到另一个视图?

[英]MVC: How can I pass a list from one view to another?

I've got some data in a view that I would like to pass to a child partial view. 我有一些数据要传递给子局部视图。 Part of that data is a list of dates that I would like to display in the partial view. 该数据的一部分是我想在部分视图中显示的日期列表。 I'm pretty sure I can't pass an IEnumerable from one view to another (when I try list is null in the controller). 我很确定我无法将IEnumerable从一个视图传递到另一个视图(当我尝试在控制器中列表为null时)。 Assuming that is the case, is there a good work around? 假设是这样,有没有好的解决方法?

I've thought about just concatenating the values into a string and then just parsing that string in the controller. 我考虑过将值串联成一个字符串,然后在控制器中解析该字符串。 That seems a bit hackish, but I think it would work. 这似乎有点骇人听闻,但我认为它将起作用。 Is there a problem with doing it like that? 这样做是否有问题? Is there a better way? 有没有更好的办法?

It just seems like such a shame to have to re-fetch the data that I've got in the parent view. 不得不重新获取我在父视图中获得的数据似乎很可耻。 I'm hoping there's another way to do it. 我希望有另一种方法。

Update: This is the model for the partial view: 更新:这是局部视图的模型:

public class SiteVisitDetailModel
{
    public String URL
    {
        get;
        set;
    }

    public List<DateTime> Dates
    {
        get;
        set;
    }
}

And this is the code from the parent view to add the partial view: 这是从父视图添加部分视图的代码:

<% Html.Telerik().PanelBar().Name("PanelBar").HtmlAttributes(new { style = "padding-left: 0em;" }).Items(items =>
{
    foreach (var item in Model.Visits)
    {
        SiteVisitDetailModel model = new SiteVisitDetailModel();
        model.URL = item.Key;
        model.Dates = (from siteVisit in item
                             select siteVisit.Time).ToList();

        items.Add()
            .Text(item.Key.ToString() + " " + item.Count().ToString() + " visits")
            .LoadContentFrom("SiteViewDetail", "Report", model);        

    }
}).Render();

In the SiteVisitDetail action method, model.URL is properly set, and model.Dates is null. 在SiteVisitDetail操作方法中,正确设置了model.URL,并且model.Dates为null。

If I understood your problem correctly... 如果我正确理解您的问题...

If your partial view can be strongly typed, its model could be the list, and you can do: 如果可以强类型化部分视图,则其模型可以是列表,您可以执行以下操作:

<%Html.RenderPartial("PartialView",myList);%>

Otherwise, the parent view can add the list to its ViewData , which would be accessible from the partial view. 否则,父视图可以将列表添加到其ViewData ,这可以从部分视图访问。

Check this post out on how to pass models around. 查看这篇文章,了解如何传递模型。

Essentially you shoiuld probably pass a model to the view that contains your list. 本质上,您应该可以将模型传递给包含列表的视图。 then you can extend it later on. 那么您可以稍后扩展它。

Multiple models sent to a single view instance 将多个模型发送到单个视图实例

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

相关问题 如何在ASP.MVC中将对象从一个视图传递到另一个视图 - How to pass an object from one view to another in ASP.MVC 如何将ID从一个视图传递到另一个MVC4 - How to pass Id from One View to Another MVC4 如何在MVC中将值从一个视图传递到另一个视图? - How to pass value from one view to another in MVC? 如何在ASP.net MVC的另一个视图中从一个视图显示摘要数据? - How can I show summary data from one view in another view in ASP.net MVC? 如何在Azure上运行的MVC 3中将会话信息从一个屏幕传递到另一个屏幕 - How can I pass session information from one screen to another in MVC 3 running on Azure Pro Asp.net core MVC 如何创建视图列表并将数据列表传递给控制器​​(如何将数据列表从视图传递给控制器​​) - Pro Asp.net core MVC how create list of view and pass the data list to the controllers (How can i pass list of data from view to controllers) 我如何在MVC的复选框中将值从Controller传递到View - How i can pass value from Controller to View in a Checkbox in MVC 如何使用session mvc将多个值从一个视图传递到另一个视图 - how to pass the multiple values from one view to another view using session mvc 如何在 MVC 3 中将列表从控制器传递到视图 - How to pass List from Controller to View in MVC 3 如何将列表从视图传递到 MVC 控制器 - How to pass a list from view to a MVC controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM