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
}
公司的數據顯示在表中,但如何顯示在下降模型的數據下。
從哪裏list.Add來了,因爲它不存在於當前的情況下 – raja
好吧,我明白了。謝謝 – raja
回答sahi lage糾正馬克卡爾dena thora。 – Saronyo