在为@Html.DropDownListFor 使用 ViewData 时无法从 'System.Collections.Generic.IEnumerable<System.Web.Mvc.SelectListItem>' 转换为 'string'
cannot convert from 'System.Collections.Generic.IEnumerable<System.Web.Mvc.SelectListItem>' to 'string' while using ViewData for @Html.DropDownListFor
我发现了很多关于相同问题的帖子,但是 none 其中有视图数据并且与存储库相关联。我只需要使用一个 class,Countries
和 Gender
不需要单独的 class。
错误:
控制器
[HttpGet]
public ActionResult Member(int? MemberId)
{
if (MemberId == null)
{
Member m = new Member();
ViewData["CountriesList"] = _dal.GetCountryList();
ViewData["GenderList"] = _dal.GetGenderList();
return View(m);
}
var data = _dal.GetMemberData(MemberId);
if (data == null)
{
return View();
}
return View(data);
}
Class
public string Country { get; set; }
public string Gender { get; set; }
public string Address { get; set; }
public int Phone { get; set; }
public IEnumerable<SelectListItem> _CountriesList { get; set; }
public IEnumerable<SelectListItem> _GenderList { get; set; }
查看
<div class="col-md-8">
<div class="col-md-4">
<label id="country">Country</label>
@Html.DropDownListFor(m => m.Country, Model._CountriesList, ViewData["CountriesList"] as IEnumerable<SelectListItem>, new { @class = "form-control", placeholder = "-- Select Country --" })
</div>
<div class="col-md-4">
<label id="gender">Gender</label><br />
@Html.DropDownListFor(m => m.Gender, Model._GenderList, ViewData["GenderList"] as IEnumerable<SelectListItem>, new { @class = "form-control", placeholder = "-- Select Gender --" })
</div>
</div>
存储库
[public IEnumerable<SelectListItem> GetCountryList()
{
var _country = new List<SelectListItem>();
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("sp_CountryList", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Flag", "getCountryList");
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
_country.Add(new SelectListItem { Text = sdr["CountryName"].ToString(), Value = sdr["rowId"].ToString() });
}
con.Close();
}
return _country;
}]
您似乎试图同时将两个数据源设置为 DropDownList,即 Model._CountriesList
和 ViewData["CountriesList"]
,这会导致构建错误。没有DropDownListFor
这样的重载方法,必须选择ViewBag
或Model
中的一种。
据我了解,您不需要使用 _CountriesList
或 _GenderList
,因此只需删除 Model._CountriesList
,因为您使用 ViewBag
:
<div class="col-md-8">
<div class="col-md-4">
<label id="country">Country</label>
@Html.DropDownListFor(m => m.Country, ViewData["CountriesList"] as IEnumerable<SelectListItem>, new { @class = "form-control", placeholder = "-- Select Country --" })
</div>
<div class="col-md-4">
<label id="gender">Gender</label><br />
@Html.DropDownListFor(m => m.Gender, ViewData["GenderList"] as IEnumerable<SelectListItem>, new { @class = "form-control", placeholder = "-- Select Gender --" })
</div>
</div>
有关详细信息,请查看 DropDownListFor
的重载
我发现了很多关于相同问题的帖子,但是 none 其中有视图数据并且与存储库相关联。我只需要使用一个 class,Countries
和 Gender
不需要单独的 class。
错误:
控制器
[HttpGet]
public ActionResult Member(int? MemberId)
{
if (MemberId == null)
{
Member m = new Member();
ViewData["CountriesList"] = _dal.GetCountryList();
ViewData["GenderList"] = _dal.GetGenderList();
return View(m);
}
var data = _dal.GetMemberData(MemberId);
if (data == null)
{
return View();
}
return View(data);
}
Class
public string Country { get; set; }
public string Gender { get; set; }
public string Address { get; set; }
public int Phone { get; set; }
public IEnumerable<SelectListItem> _CountriesList { get; set; }
public IEnumerable<SelectListItem> _GenderList { get; set; }
查看
<div class="col-md-8">
<div class="col-md-4">
<label id="country">Country</label>
@Html.DropDownListFor(m => m.Country, Model._CountriesList, ViewData["CountriesList"] as IEnumerable<SelectListItem>, new { @class = "form-control", placeholder = "-- Select Country --" })
</div>
<div class="col-md-4">
<label id="gender">Gender</label><br />
@Html.DropDownListFor(m => m.Gender, Model._GenderList, ViewData["GenderList"] as IEnumerable<SelectListItem>, new { @class = "form-control", placeholder = "-- Select Gender --" })
</div>
</div>
存储库
[public IEnumerable<SelectListItem> GetCountryList()
{
var _country = new List<SelectListItem>();
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("sp_CountryList", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Flag", "getCountryList");
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
_country.Add(new SelectListItem { Text = sdr["CountryName"].ToString(), Value = sdr["rowId"].ToString() });
}
con.Close();
}
return _country;
}]
您似乎试图同时将两个数据源设置为 DropDownList,即 Model._CountriesList
和 ViewData["CountriesList"]
,这会导致构建错误。没有DropDownListFor
这样的重载方法,必须选择ViewBag
或Model
中的一种。
据我了解,您不需要使用 _CountriesList
或 _GenderList
,因此只需删除 Model._CountriesList
,因为您使用 ViewBag
:
<div class="col-md-8">
<div class="col-md-4">
<label id="country">Country</label>
@Html.DropDownListFor(m => m.Country, ViewData["CountriesList"] as IEnumerable<SelectListItem>, new { @class = "form-control", placeholder = "-- Select Country --" })
</div>
<div class="col-md-4">
<label id="gender">Gender</label><br />
@Html.DropDownListFor(m => m.Gender, ViewData["GenderList"] as IEnumerable<SelectListItem>, new { @class = "form-control", placeholder = "-- Select Gender --" })
</div>
</div>
有关详细信息,请查看 DropDownListFor