ASP.NET MVC-如何将下拉列表绑定方法调用到另一个操作方法

ASP.NET MVC-如何将下拉列表绑定方法调用到另一个操作方法,asp.net,asp.net-mvc,drop-down-menu,Asp.net,Asp.net Mvc,Drop Down Menu,这是我的行动方法 public ActionResult getCountry() { mstrClient objClientList = new mstrClient(); DataSet ds = new DataSet(); string sQuery = "SELECT * FROM INV_mstrCountry WHERE CStatus = 'Y'"; SqlDataAdapter da = new Sql

这是我的行动方法

 public ActionResult getCountry()
    {
        mstrClient objClientList = new mstrClient();
        DataSet ds = new DataSet();
        string sQuery = "SELECT * FROM INV_mstrCountry WHERE CStatus = 'Y'";
        SqlDataAdapter da = new SqlDataAdapter(sQuery, con);
        con.Open();
        da.Fill(ds);
        con.Close();
        List<mstrClient> CountryList = new List<mstrClient>();
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            mstrClient objClient = new mstrClient();
            objClient.CountryCode = ds.Tables[0].Rows[i]["CountryCode"].ToString();
            objClient.Country = ds.Tables[0].Rows[i]["CountryName"].ToString();
            CountryList.Add(objClient);
        }
        objClientList.getCountryList = CountryList;
        return View(objClientList);
    }

如何在MVC中调用视图中的下拉列表
首先更改方法以返回对象而不是视图:

public List<mstrClient> getCountry()
{
    mstrClient objClientList = new mstrClient();
    DataSet ds = new DataSet();
    string sQuery = "SELECT * FROM INV_mstrCountry WHERE CStatus = 'Y'";
    SqlDataAdapter da = new SqlDataAdapter(sQuery, con);
    con.Open();
    da.Fill(ds);
    con.Close();
    List<mstrClient> CountryList = new List<mstrClient>();
    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
        mstrClient objClient = new mstrClient();
        objClient.CountryCode = ds.Tables[0].Rows[i]["CountryCode"].ToString();
        objClient.Country = ds.Tables[0].Rows[i]["CountryName"].ToString();
        CountryList.Add(objClient);
    }
    return CountryList;
}
查看:

public ActionResult Create()
{
    mstrClient objClient = new mstrClient();
    ModelState.Clear();
    var list = getCountry();
    ViewBag.CountryList = new SelectList(list, "ID_VALUE", "TEXT_ON_DROPDOWNLIST");
    return View(objClient);
}
 <div class="row">
                <div class="col-md-12">
                    <div class="form-group">
                        <label>Select a item from the dropdownlist</label>
                        @Html.DropDownList("CountryList", null, htmlAttributes: new { @class = "form-control" })
                    </div>
                </div>
            </div>

从下拉列表中选择一个项目
@DropDownList(“CountryList”,null,htmlAttributes:new{@class=“form control”})

您只需要mstrClient的列表吗?在这种情况下,您可以将其存储在var中并在创建方法中使用Hia Ivan san can ypu plz发送了示例代码Hi,如果您可以共享该类,我可以帮助您更好地工作,非常感谢您san,但有一个问题,在提交数据时显示错误,类似于没有具有键“ProductList”的“IEnumerable”类型的ViewData项。如果返回视图,则需要再次调用ViewBag,因为它是动态的,并且仅在当前http请求期间持续。如果发生重定向,ViewData值将被清除。哦,您需要将ViewBag.CountryList命名为类上属性的ID
 <div class="row">
                <div class="col-md-12">
                    <div class="form-group">
                        <label>Select a item from the dropdownlist</label>
                        @Html.DropDownList("CountryList", null, htmlAttributes: new { @class = "form-control" })
                    </div>
                </div>
            </div>