ASP.NET MVC每行下拉列表从数据库获取数据

时间:2017-08-10 08:47:48

标签: jquery html sql-server ajax asp.net-mvc

我的数据表的每一行都有下拉列表,数据表的内容来自tblaccount,然后下拉列表的内容来自tblclient

tblclient

cid     cname        datereg
1       Client 1     08/07/2017
2       Client 2     08/08/2017
3       Client 3     08/10/2017

enter image description here enter image description here

我需要帮助,当我在下拉列表中选择一个项目时,它必须从数据库中获取所选客户端名称的客户端ID和日期,并将其显示在同一行中。 3天,我还不能这样做,请帮忙。

ListAccount.chtml

@model MyProject.Models.ModAccountAndClient

<table class="table table-bordered data-table">
    <thead>
    <tr>
        <th>ID</th>
        <th>CODE</th>
        <th>NAME</th>
        <th>CLIENT NAME</th>
        <th>CLIENT ID</th>
        <th>DATE REGISTERED</th>
    </tr>
    </thead>
    <tbody>
    @foreach (var item in Model.Accounts)
    {
        <tr>
            <td>@item.aid</td>
            <td>@item.acode</td>
            <td>@item.aname</td>
            <td>
                <select>
                @foreach (var item in Model.Clients)
                    {
                        <option>@item.cname</option>
                    }
                </select>
            </td>
            <td></td>
            <td></td>
        </tr>
    }
    </tbody>
</table>

的HomeController

public ActionResult ListAccount()
{
    ModAccountAndClient list = new ModAccountAndClient();
    list.Accounts = _repo.ListAccount();
    list.Clients = _repo.ListClient();
    return View(list);
}

ModConnection

public List<ModAccount> ListAccount()
{
    var list = new List<ModAccount>();
    SqlCommand cmd = new SqlCommand("SELECT * FROM tblaccount", con);
    con.Open();
    SqlDataReader rdr = cmd.ExecuteReader();
    while (rdr.Read())
    {
        var param = new ModAccount()
        {
            aid = (int)rdr["aid"],
            acode = rdr["acode"] as string,
            aname = rdr["aname"] as string
        };
        list.Add(param);
    }
    con.Close();
    return list;
}

public List<ModClient> ListClient()
{
    var list = new List<ModClient>();
    SqlCommand cmd = new SqlCommand("SELECT * FROM tblclient", con);
    con.Open();
    SqlDataReader rdr = cmd.ExecuteReader();
    while (rdr.Read())
    {
        var param = new ModClient()
        {
            cid = (int)rdr["cid"],
            cname = rdr["cname"] as string,
            cdatereg = rdr["cdatereg"] as string
        };
        list.Add(param);
    }
    con.Close();
    return list;
}

0 个答案:

没有答案