2017-10-06 105 views
0

我有查看索引。在索引視圖中,我有html下拉菜單和表格。我想從Job模型和公司模型表中填入下拉列表。我使用Join查詢,它給我所需的結果。但我不知道如何傳遞數據來查看和顯示下拉菜單和表格。.net MVC顯示結果從查看結果查看

這是我的工作模型類。

 public class Job 
{ 

    public List<Job> joe { get; set; } 

    [Required] 

    public string id { get; set; } 

    [Required] 

    public string job_title { get; set; } 

    [Required] 

    public string job_description { get; set; } 

    [Required] 

    public string company_id{ get; set; } 


    } 

這是我的公司模型類。

public class Company 
    { 
    public List<Company> jo { get; set; } 

    [Required] 

    public string id { get; set; } 


    [Required] 

    public string name { get; set; } 

    [Required] 

    public string address { get; set; } 

}

我的控制器功能。

 public ActionResult ViewData (string id) 
    { 
     TempData["data"] = id; 

     SqlDataReader dr; 

     SqlConnection con = new SqlConnection("xxxxxxxx"); 
     SqlCommand cmd = new SqlCommand(); 
     con.Open(); 
     cmd.CommandText = "SELECT name,address,job_title FROM Company JOIN 
     JOIN Jobs on Jobs.id=Company.id where Jobs.company_id='" + id + "'"; 

     cmd.Connection = con; 



     var model = new List<Company>(); 
     try 
     { 
      dr = cmd.ExecuteReader(); 
      // giving me Required result but how to pass required result to 
      //single view as I ma getting data from two tables 

      while (dr.Read()) 
      { 

       var candidates = new Company(); 

       candidates.name = dr[0].ToString(); 
       candidates.address = dr[1].ToString(); 
       candidates.job_title= dr[2].ToString(); 
       candidates.job_description= dr[3].ToString(); 


       model.Add(candidates); 
      } 
     } 

     catch (Exception es) 
     { 
      throw es; 
     } 

     finally 
     { 

      con.Close(); 
     } 

     return View(model); 

     } 

而我的視圖來顯示錶中的數據是

 @model IEnumerable<Models.Company> 

     <table id="movie" class="table table-striped"> 
     <thead>      
     <tr> 
     <th>Name</th> 
        <th>Address</th> 

      </tr> 
     </thead> 
     <tbody> 
       @foreach(var j in Model) 
       { 
        <tr> 
         <td>@Html.DisplayFor(modelItem => j.name)</td> 
         <td>@Html.DisplayFor(modelItem => j.address)</td> 
         </td> 
        </tr> 

       } 

      </tbody> 
      <div> 

      </div> 
      </table> 

和我的下拉列表已

  @foreach(var j in Model) 
       { 
        // drop down to show job_title 

       } 

公司的數據顯示在表中,但如何顯示在下降模型的數據下。

回答

0

我改變了查詢的一些部分。將操作方法​​名稱更改爲Index。這是操作方法的變化部分。

 cmd.CommandText = "SELECT name,address,job_title FROM Company JOIN Job on Job.COMPANY_ID = Company.id where Job.company_id = '" + id + "'"; 
     cmd.Connection = con; 

     var model = new List<Company>(); 
     try 
     { 
      dr = cmd.ExecuteReader(); 
      List<SelectListItem> list = new List<SelectListItem>(); 
      // giving me Required result but how to pass required result to 
      //single view as I am getting data from two tables 
      while (dr.Read()) 
      { 
       var candidates = new Company(); 
       candidates.name = dr[0].ToString(); 
       candidates.address = dr[1].ToString(); 
       candidates.job_title = dr[2].ToString(); 
       list.Add(new SelectListItem { Selected = true, Text = dr[2].ToString(), Value = dr[2].ToString() }); 
       model.Add(candidates); 
      } 

      ViewBag.jobList = list; 

     } 

該視圖部分。

@foreach (var j in Model) 
    { 
     <tr> 
      <td>@Html.DisplayFor(modelItem => j.name)</td> 
      <td>@Html.DisplayFor(modelItem => j.address)</td>  
      <td>@Html.DropDownList("jobList")</td> 
     </tr> 
    } 

還有很多其他更好的方法。但是我爲了簡單的目的做了它。

+0

從哪裏list.Add來了,因爲它不存在於當前的情況下 – raja

+0

好吧,我明白了。謝謝 – raja

+0

回答sahi lage糾正馬克卡爾dena thora。 – Saronyo