0
votes

How to get data from two table or two class in single view in mvc3. I want to show data from both the table. I have one parent class and two child class. i.e public class GetDocumentParent {enter code here public List getDocument { get; set; } public List getDocumentRevision { get; set; } }

View:

" %>
<% 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 and item.ActiveDate from Document Class.

item.Revision from Document_Revision class.

how to show both class's value at the same time in view.

Controller:

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

             };
             return View(model);

Model: 1.

public class DOCUMENTS {

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

2.

public class DOCUMENT_REVISIONS { public string REVISION { get; set; } }

Thanks in advance

2

2 Answers

1
votes

Always remember that it is best to have a view model that represents your data that is sent to the view. The view will do what is needed with you view model. The code below can guide you, you must just change it to fit into your scenario.

Lets say that I have a customer and each customer has only 1 address. I would create a view model called CustomerDetailsViewModel to represent this data:

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; }
}

Your action method to display the customer and customer address details:

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 above represents your customer's unique identifier. It is used to return a cusotmer:

Customer customer = customerRepository.GetById(id);

and also used to returned a specific customer's address:

Address address = customerRepository.GetCustomerAddress(id);

An ICustomerRepository instance is injected by an IoC container for example Autofac.

In your view you pass this view model as such and can display the data as you please:

@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>

I hope this helps.

0
votes

What is the problem? use in controller:

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

in View:

@Html.EditorFor(m=>m.getDocument);
@Html.EditorFor(m=>m.getDocumentRevision);