.net MVC在连接查询的视图中显示结果

时间:2017-10-06 11:14:17

标签: asp.net-mvc join

我查看索引。在索引视图中我有html下拉和表格。我想通过公司模型中的Job模型和表填充下拉列表。我使用了Join查询,它给了我所需的结果。但我不知道如何传递数据来查看并显示下拉和表格。

这是我的工作模型课。

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


    }

这是我的公司Model class。

    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 

              }

公司数据显示在表格中,但如何在下拉列表中显示模型数据。

1 个答案:

答案 0 :(得分: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>
    }

还有很多其他更好的方法。但我这样做是为了简化目的。