这是我的动作方法,
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 ActionResult Create()
{
mstrClient objClient = new mstrClient();
ModelState.Clear();
return View(objClient);
}
答案 0 :(得分:0)
如何在MVC中的视图中调用DropDownList。
首先更改方法以返回对象而不是视图:
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>