0

如何在 mvc3 的单个视图中从两个表或两个类中获取数据。我想显示两个表中的数据。我有一个父类和两个子类。即 public class GetDocumentParent { enter code here public List getDocument { get; 放; } 公共列表 getDocumentRevision { 获取;放; } }

看法:

" %>
<% foreach (var item in Model.getDocument)
   { %>


        <tr>
            <td>

             <%:Html.DisplayFor(ModelState => item.DOCUMENT_NAME)%>
            </td>
            <td>
              <%:Html.DisplayFor(ModelState => item.ActiveDate)%>
            </td>    
            <td>
                <%:Html.DisplayFor(ModelState => item.Revision)%>
            </td>
             <td>
      </tr>

<% } %>

==>item.DOCUMENT_NAME 和 item.ActiveDate 来自文档类。

来自 Document_Revision 类的 item.Revision。

如何在视图中同时显示两个类的值。

控制器:

var model = new GetDocumentParent
             {
                 getDocument = db.Document.ToList(),
                 getDocumentRevision = db.DocumentRevisions.ToList()

             };
             return View(model);

型号:1。

公共类文档{

     public string DOCUMENT_NAME
      {
        get;
        set;
      }
    public Nullable<System.DateTime> ActiveDate
      {
        get;
        set;
      }
}

2.

公共类 DOCUMENT_REVISIONS { 公共字符串 REVISION { 获取;放; } }

提前致谢

4

2 回答 2

1

永远记住,最好有一个视图模型来表示发送到视图的数据。该视图将执行您的视图模型所需的操作。下面的代码可以指导您,您必须更改它以适应您的场景。

假设我有一个客户,每个客户只有一个地址。我会创建一个视图模型CustomerDetailsViewModel来表示这些数据:

public class CustomerDetailsViewModel
{
     public string FirstName { get; set; }

     public string Lastname { get; set; }

     public int Age { get; set; }

     public string AddressLine1 { get; set; }

     public string AddressLine2 { get; set; }

     public string City { get; set; }
}

您显示客户和客户地址详细信息的操作方法:

public class CustomerController : Controller
{
     private readonly ICustomerRepository customerRepository;

     public CustomerController(ICustomerRepository customerRepository)
     {
          // Check customerRepository for nulls

          this.customerRepository = customerRepository;
     }

     public ActionResult Details(int id)
     {
          // Check that id is not zero or negative

          Customer customer = customerRepository.GetById(id);

          // Get the customer's address
          Address address = customerRepository.GetCustomerAddress(id);

          CustomerDetailsViewModel viewModel = new CustomerDetailsViewModel()
          {
               FirstName = customer.Firstname,
               LastName = customer.LastName,
               Age = customer.Age,
               AddressLine1 = address.AddressLine1,
               AddressLine2 = address.AddressLine2,
               City = address.City
          }

          return View(viewModel);
     }
}

id以上代表您客户的唯一标识符。它用于返回客户:

Customer customer = customerRepository.GetById(id);

并且还用于返回特定客户的地址:

Address address = customerRepository.GetCustomerAddress(id);

一个ICustomerRepository实例由IoC container例如Autofac注入。

在您的视图中,您可以这样传递此视图模型,并且可以随意显示数据:

@model MyProject.DomainModel.ViewModels.Customers.CustomerDetailsViewModel

<div class="content">

     <div class="display-label">First Name: </div>
     <div class="display-content">@Model.FirstName</div>

     <div class="display-label">Last Name: </div>
     <div class="display-content">@Model.LastName</div>

     <div class="display-label">Age: </div>
     <div class="display-content">@Model.Age</div>

     <div class="display-label">Address Line 1: </div>
     <div class="display-content">@Model.AddressLine1</div>

     <div class="display-label">Address Line 2: </div>
     <div class="display-content">@Model.AddressLine2</div>

     <div class="display-label">City: </div>
     <div class="display-content">@Model.City</div>

</div>

我希望这有帮助。

于 2012-08-27T11:44:35.033 回答
0

问题是什么?在控制器中使用:

var model = new GetDocumentParent();
model.getDocument = ...//Code that initialize getDocument
model.getDocumentRevision =...//Code that initialize getDocumentRevision

在视图中:

@Html.EditorFor(m=>m.getDocument);
@Html.EditorFor(m=>m.getDocumentRevision);
于 2012-08-27T11:24:28.377 回答