[英]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.