2014-12-02 69 views
0

我的问题是列表<>,我在两个列表中控制器操作方法采取两个模型它传递两个列表中的值,但它不能传递两个列表值在视图中,我给我下面的代码,请给一些解决方案这个。如何在mvc 4的一个视图中传递两个模型列表?

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> 
+0

[视图中的多个型号(可能重复的http://计算器。 com/questions/4764011 /多模型在视图) – DavidG 2014-12-02 11:52:42

回答

2

这很容易。创建一个包含2个列表的类。

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

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

然后在控制器返回的CustomModel

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

并在视图

@model Myproject.Models.CustomModel 
+0

感谢您的答复先生,我想添加列表和列表值。 – 2014-12-02 12:21:26

+0

@Gowthamg你能解释一下自己吗?我不明白你的问题 – Bonomi 2014-12-03 10:37:47

+0

感谢Bonimi,我明白了,我得到了输出,再次感谢回复和答复。 – 2014-12-05 12:04:05

相关问题