[英]cannot convert from 'System.Collections.Generic.IEnumerable<System.Web.Mvc.SelectListItem>' to 'string' while using ViewData for @Html.DropDownListFor
I've found many posts regarding the same questions but none of them were with view data and connected with a repository.我发现很多关于相同问题的帖子,但没有一个是与视图数据相关的并且与存储库相关联。 I need to use only one class no separate class for
Countries
and Gender
, please.我只需要使用一个 class 没有单独的 class 用于
Countries
和Gender
,请。
Error:错误:
Controller Controller
[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 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; }
View看法
<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>
Repository存储库
[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;
}]
It seems you are trying to set two datasources to DropDownList at same time which are Model._CountriesList
and ViewData["CountriesList"]
and it causes build error.您似乎正在尝试同时将两个数据源设置为 DropDownList,它们是
Model._CountriesList
和ViewData["CountriesList"]
,它会导致构建错误。 There is no such a overload method of DropDownListFor
, you have to choose one of them ViewBag
or Model
.没有
DropDownListFor
这样的重载方法,您必须选择其中一个ViewBag
或Model
。
As I understood correctly, you don't need to use _CountriesList
or _GenderList
so just remove Model._CountriesList
since you use ViewBag
:正如我正确理解的那样,您不需要使用
_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>
For more info, have a look at overloads of DropDownListFor
有关更多信息,请查看
DropDownListFor
的重载
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.