简体   繁体   English

无法从 'System.Collections.Generic.IEnumerable 转换<system.web.mvc.selectlistitem>在将 ViewData 用于 @Html.DropDownListFor 时,将 ' 转换为 'string'</system.web.mvc.selectlistitem>

[英]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 用于CountriesGender ,请。

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._CountriesListViewData["CountriesList"] ,它会导致构建错误。 There is no such a overload method of DropDownListFor , you have to choose one of them ViewBag or Model .没有DropDownListFor这样的重载方法,您必须选择其中一个ViewBagModel

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.

相关问题 如何解决“'System.Collections.Generic.List<string> ' 到 'System.Collections.Generic.List<system.web.mvc.selectlistitem> ''</system.web.mvc.selectlistitem></string> - how to resolve "'System.Collections.Generic.List<string>' to 'System.Collections.Generic.List<System.Web.Mvc.SelectListItem>'' 无法从“ System.Web.UI.ControlCollection”转换为“ System.Collections.Generic.IEnumerable” <System.Web.UI.Control> &#39; - cannot convert from 'System.Web.UI.ControlCollection' to 'System.Collections.Generic.IEnumerable<System.Web.UI.Control>' 无法将类型&#39;System.Web.Http.Results.NotFoundResult&#39;隐式转换为&#39;System.Collections.Generic.IEnumerable &lt;...&gt;&#39; - Cannot implicitly convert type 'System.Web.Http.Results.NotFoundResult' to 'System.Collections.Generic.IEnumerable<…>' SignalR:无法从System.String转换或转换为System.Collections.Generic.IEnumerable - SignalR : Could not cast or convert from System.String to System.Collections.Generic.IEnumerable 无法将类型&#39;System.Collections.Generic.IEnumerable隐式转换为System.Collections.Generic.IEnumerable <TiendaDeportes.producto> - Cannot implicitly convert type 'System.Collections.Generic.IEnumerable To System.Collections.Generic.IEnumerable<TiendaDeportes.producto> 无法隐式转换类型&#39;System.Collections.Generic.List <AnonymousType#1> &#39;到&#39;System.Collections.Generic.IEnumerable - Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.IEnumerable 无法隐式转换类型&#39;System.Collections.Generic.IEnumerable <System.DateTime> &#39;到&#39;System.DateTime&#39; - Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.DateTime>' to 'System.DateTime' ''System.Web.Mvc.SelectListItem' 不包含'string'的定义' - ''System.Web.Mvc.SelectListItem' does not contain a definition for 'string'' 如何使用System.Web.Mvc.SelectListItem - How to use System.Web.Mvc.SelectListItem 无法将类型&#39;System.Linq.IQueryable&#39;隐式转换为&#39;System.Collections.Generic.IEnumerable&#39; - Cannot implicitly convert type ‘System.Linq.IQueryable’ to ‘System.Collections.Generic.IEnumerable’
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM