0

My problem is list<>,i am taking two model in two list in controller action method it pass values in two lists but it can not pass two list values in view,i am giving my code below,please give some solution for this.

    projet name Myproject,

    my model,

    public class table1
    {
      public int id {get;set;}
      public string student_name {get;set;}
    }
    public class table2
    {
      public int id {get;set;}
      public string roll_number {get;set;}
    }

    my controller page,

    list<table1> t=new list<table1>();
    list<table2> t1=new list<table2>();
    public ActionResult details()
    {
       sqlDataAdapter da=new sqldataAdapter("select * from Table1",con);
       dataset ds=new dataset();
       da.fill(ds);
       foreach(datarow dr in ds.Table[0].row)
       {
         t.add(new table1()
          {
            id=int.parse(dr[0].ToString()),
            student_name=dr[1].ToString()
          }
       }

       sqlDataAdapter da1=new sqldataAdapter("select * from Table2",con);
       dataset ds1=new dataset();
       da1.fill(ds1);
       foreach(datarow dr1 in ds1.Table[0].row)
       {
         t1.add(new table2()
          {
            id=int.parse(dr1[0].ToString()),
            roll_number=dr1[1].ToString()
          }
       }

       return details(t,t1);
    }

    view   \\\\\\\\\\\\\\\\ My problem in view ,plase help me this problem.

    @using MultiSelectList;
    @model  List<Myproject.Models.table1>
    @model  List<Myproject.Models.table2>


    @{
        ViewBag.Title = "Details";
    }

    <h1>Welcome to details page</h1>
Gowtham g
  • 29
  • 1
  • 8
  • possible duplicate of [Multiple models in a view](http://stackoverflow.com/questions/4764011/multiple-models-in-a-view) – DavidG Dec 02 '14 at 11:52

1 Answers1

2

It's easy. Create a class that includes the 2 lists.

public class CustomModel
    {
        public List<table1> Table1 { get; set; }

        public List<table2> Table2 { get; set; }
    }

And then in the controller you return the CustomModel

return details(t,new CustomModel { Table1 = t1, Table2 = t2 });

And in the view

@model  Myproject.Models.CustomModel
Bonomi
  • 2,541
  • 5
  • 39
  • 51