Click here to Skip to main content
15,911,306 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please Send Me Demo Code To Generate Strongly Typed View While Data Fetching is Done From Two Tables.

I.E. User_Master-->RoleID
Role_Master-->RoleID


...,
User_Master.Name,
Role_Master.RoleName

...,
Posted

1 solution

You can't do that directly but you can create a ViewModel class with two properties on it that hold references to your table. You strongly type the View against that ViewModel class.

ViewModel:
SQL
public class ViewModelTables
{
   public MyTable customer {get; set;}
   public MyOtherTable MyOtherTable {get; set;}
}


View:
C#
<![CDATA[<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<ViewModelTables>" %>]]>

<![CDATA[<% foreach(var tab1Item in Model.customer)
   { %>]]>
   // render here what ever you want to render
   <%: Html.TextboxFor(name => tab1Item.Name) %>
<![CDATA[<% } %>]]>

<![CDATA[<% foreach(var tab2Item in Model.MyOtherTable)
   { %>]]>
   // render here what ever you want to render
<![CDATA[<% } %>]]>


Controller:
C#
public ActionResult MyDoubleTables()
{
   var my2Tab = new ViewModelTables();

   var tab1 = GetTable1(); // whatever you need to do
   var tab2 = GetTable2(); // whatever you need to do

   my2Tab.MyTable = tab1;
   my2Tab.MyOtherTable = tab2;

   return View(my2tab);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900