简体   繁体   English

Asp.Net MVC-视图->创建2个对象

[英]Asp.Net MVC - View -> Create 2 objects

First of all, I'm really new to the MVC Asp.Net ideology. 首先,我对MVC Asp.Net意识真的很陌生。

I would like to know how can I create two objects (model) into one view ? 我想知道如何在一个视图中创建两个对象(模型)?

because if I look at the view header it's inherit from one model : 因为如果我查看视图头,它是从一个模型继承的:

\\<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %> \\ <%@页面标题=“”语言=“ C#” MasterPageFile =“〜/ Views / Shared / Site.Master” Inherits =“ System.Web.Mvc.ViewPage”%>

So, if, for example, I want to create in the same view (Aspx page) an MyObjectA and an object MyObjectB, what's the best way to handle that ? 因此,例如,如果我想在同一视图(Aspx页面)中创建MyObjectA和对象MyObjectB,那么处理该问题的最佳方法是什么?

I hope I've been clear ... 我希望我已经清楚了...

If you have a model object Person and another lets say Comment and then in the same view you would like to display a persons details and comments added to that person you may want to create an intermediate object sometimes referred to as a 'data transfer object' or 'view object'. 如果您有一个模型对象“ Person ,另一个让我们说“ Comment ,然后在同一视图中要显示人的详细信息和添加到该人的注释,则可能需要创建一个中间对象,有时称为“数据传输对象”或“查看对象”。 So, i create a simple class: 因此,我创建了一个简单的类:

public class PersonDetailDTO
{
    public Person PersonDetail {get; set;}
    public IList<Comment> Comments {get; set;}
}

.. now i can return the result of my action as type PersonDetailDTO instead of say Person . ..现在我可以将我的操作结果返回为PersonDetailDTO类型,而不是说Person Then the view is strongly typed to PersonDetailDTO also, making it easy for me to access the PersonDetail data and Comments collection. 然后,将视图也强类型PersonDetailDTO ,这使我很容易访问PersonDetail数据和Comments集合。

For example, i use a view object like this for one of my partial views: 例如,我对部分视图之一使用这样的视图对象:

        public class AnnouncementsPartialViewData
        {
            public IList<Announcement> Announcements { get; set; }
            public object MonthlyPlannerRouteVals { get; set; }
            public object PreSchoolRouteVals { get; set; }
            public object ElementaryRouteVals { get; set; }
        }

.. and the partial view header looks like this: ..和局部视图标题看起来像这样:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<KsisOnline.Web.Controllers.HomeController.IndexViewData.AnnouncementsPartialViewData>" %>

.. and so i can access the typed data from that view class in the view easily like so: ..因此我可以轻松地从视图中的该视图类访问类型化的数据,如下所示:

<% if (Model.Announcements.Count == 0)

If by 'creating' you mean 'passing' two objects from a controller to a view, you should create a new class that would contain the two objects and pass it from the controller to the view: 如果通过“创建”是指将两个对象从控制器“传递”到视图,则应创建一个包含两个对象的新类,并将其从控制器传递到视图:

public class MyModel
{
  public MyObjectA ObjectA { get; set; }
  public MyObjectB ObjectB { get; set; }
}

The View definition would then look like this: 然后,View定义将如下所示:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MyModel>" %>

In the controller, you would create the object like 在控制器中,您将创建如下对象

...(in controller action)
return new MyModel { ObjectA = new MyObjectA(), ObjectB = new MyObjectB() };

From the view, you would access the objects like 从视图中,您将访问诸如

var myObjectA = Model.ObjectA;

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

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