gpt4 book ai didi

asp.net-mvc - 如何在ASP.NET MVC中将多个对象传递给“查看页面”?

转载 作者:行者123 更新时间:2023-12-04 13:35:59 24 4
gpt4 key购买 nike

我想我知道答案,但是我想跳出一些想法。

我想将几个(在本例中为2个)略有不同的数据传递给一个View。我最初的想法只是将各种对象包装到一个包含对象中,然后按照这种方式传递它们。然后从“ View ”中,我会看到类似

var objContainer = ViewData.Model;
var thisObject = objContainer.ThisObject;
var thatObject = objContainer.ThatObject;

并且可以在“母版页”和“查看页”中单独使用它们。

这是“最佳”方式吗?

最佳答案

我发现创建附加的专用于 View 的类很有用。我将它们保留在名为“Core.Presentation”的单独命名空间中,以使事情井井有条。这是一个例子:

namespace Core.Presentation
{
public class SearchPresentation
{
public IList<StateProvince> StateProvinces { get; set; }
public IList<Country> Countries { get; set; }
public IList<Gender> Genders { get; set; }
public IList<AgeRange> AgeRanges { get; set; }
}
}

然后,确保我的View是使用该演示文稿类的通用版本的强类型 View :
public partial class Search : ViewPage<SearchPresentation>

通过这种方式,我可以在 View 中使用Intellisense并轻松浏览各个项目。

关于asp.net-mvc - 如何在ASP.NET MVC中将多个对象传递给“查看页面”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/238430/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com