MVC的新手|来自不同来源的视图中的数据

时间:2022-05-09 15:45:26

OK, So i have been watching some MVC vids and reading some bits. I am new to the entire MVC pattern, and until now have been happily wrapped up in the web forms world!

好的,所以我一直在看一些MVC视频并阅读一些内容。我是整个MVC模式的新手,直到现在已经愉快地融入了Web表单世界!

Like with so many demos it all seems great and I'm sure I'll have lots I dont understand as I move along, but in the first instance...

就像有这么多演示一样,这一切看起来都很棒,而且我确信随着时间的推移,我会有很多我不理解的东西,但首先......

I can see that you can have a strongly typed view, which gets data from the controller. What happens if I want data in a view from different object types?? Say i want to show a grid of cars and a grid of people, which are not related in anyway??

我可以看到你可以有一个强类型视图,它从控制器获取数据。如果我想要来自不同对象类型的视图中的数据,会发生什么?说我想显示一个汽车网格和一个人的网格,无论如何都不相关?

Thx Steve

4 个解决方案

#1


4  

Setup your strongly typed ViewData class with two properties like this

使用这样的两个属性设置强类型的ViewData类

public class MyViewData 
{ 
  public IEnumerable<Car> Cars { get; set; }
  public IEnumerable<People> People { get; set; }
}

and then fill them in the controller, Sorry for the duplicate. In good MVC spirit try to use interfaces where possible to make your code more generic

然后将它们填入控制器,抱歉复制。在良好的MVC精神中尝试尽可能使用接口使您的代码更通用

#2


2  

Instead of artificially grouping models together you could keep then separate (logically and physically) and then in the view pull the various pieces together.

您可以将模型分开(逻辑上和物理上),然后在视图中将各个部分组合在一起,而不是人为地将模型分组在一起。

Check out this post for the a great explanation of [link text][1].

查看这篇文章,了解[link text] [1]的一个很好的解释。

[1]: http://blog.codeville.net/2008/10/14/partial-requests-in-aspnet-mvc/ partial-requests

[1]:http://blog.codeville.net/2008/10/14/partial-requests-in-aspnet-mvc/ partial-requests

#3


0  

You can either pass both objects inside the ViewData hashtable, or create a MyViewViewModel, add two properties, and set them both from your controller.

您既可以在ViewData哈希表中传递两个对象,也可以创建一个MyViewViewModel,添加两个属性,并从控制器中设置它们。

#4


0  

What I think would be best to do in this situation would be create a class in the Models folder to hold both of these types.

我认为在这种情况下最好的做法是在Models文件夹中创建一个类来保存这两种类型。

Example:

public class CarsPeopleModel
    {
        public List<Car> Cars { get; set; }
        public List<Person> People { get; set; }
    }

Then your view would be:

然后你的观点是:

public partial class Index : ViewPage<MvcApplication1.Models.CarsPeopleModel>
    {
    }

#1


4  

Setup your strongly typed ViewData class with two properties like this

使用这样的两个属性设置强类型的ViewData类

public class MyViewData 
{ 
  public IEnumerable<Car> Cars { get; set; }
  public IEnumerable<People> People { get; set; }
}

and then fill them in the controller, Sorry for the duplicate. In good MVC spirit try to use interfaces where possible to make your code more generic

然后将它们填入控制器,抱歉复制。在良好的MVC精神中尝试尽可能使用接口使您的代码更通用

#2


2  

Instead of artificially grouping models together you could keep then separate (logically and physically) and then in the view pull the various pieces together.

您可以将模型分开(逻辑上和物理上),然后在视图中将各个部分组合在一起,而不是人为地将模型分组在一起。

Check out this post for the a great explanation of [link text][1].

查看这篇文章,了解[link text] [1]的一个很好的解释。

[1]: http://blog.codeville.net/2008/10/14/partial-requests-in-aspnet-mvc/ partial-requests

[1]:http://blog.codeville.net/2008/10/14/partial-requests-in-aspnet-mvc/ partial-requests

#3


0  

You can either pass both objects inside the ViewData hashtable, or create a MyViewViewModel, add two properties, and set them both from your controller.

您既可以在ViewData哈希表中传递两个对象,也可以创建一个MyViewViewModel,添加两个属性,并从控制器中设置它们。

#4


0  

What I think would be best to do in this situation would be create a class in the Models folder to hold both of these types.

我认为在这种情况下最好的做法是在Models文件夹中创建一个类来保存这两种类型。

Example:

public class CarsPeopleModel
    {
        public List<Car> Cars { get; set; }
        public List<Person> People { get; set; }
    }

Then your view would be:

然后你的观点是:

public partial class Index : ViewPage<MvcApplication1.Models.CarsPeopleModel>
    {
    }