【问题标题】:"Cannot convert type 'ViewModels.DropDownVM' to 'System.Web.Mvc.SelectList'" when Populating DropDown with MySQL Query“使用 MySQL 查询填充 DropDown 时,无法将类型 'ViewModels.DropDownVM' 转换为 'System.Web.Mvc.SelectList'”
【发布时间】:2015-10-02 19:31:17
【问题描述】:

我已经松散地关注了:specify name and id to Drop down razor technique asp dot net mvc4

我想要的最终结果是我有一个视图,其中包含两个下拉列表,其中填充了数据库中“ww”列中的值。一旦用户选择了开始和结束并单击提交按钮,它会将选定的值传递给另一个ActionMethod,然后该ActionMethod 获取这些值并执行类似的查询,并指向具有不同控件集的不同视图。

但是,我得到了一个

"无法将类型 'ViewModels.DropDownVM' 转换为 'System.Web.Mvc.SelectList'"

我的代码中的以下行出现错误,并且无法弄清楚如何解决该问题以使一切顺利进行。

@Html.DropDownList("WWStart", (SelectList)ViewBag.DDLWWStart, " -- Select Starting Work Week -- ")

我有以下几点:

ViewModels/DropDownVM.cs sn-p:

public class DropDownVM
{
    public int SelectedCategory { get; set; }
    //public string CategoryName { get; set; }
    public SelectList Categories { get; set; }
}

IndicatorController 索引():

    public ActionResult Index()
    {
        using (var context = new taskDBContext())
        {
            var DDLWWQuery = (from wq in context.taskSet
                              select new
                              {
                                  wq.ww
                              }).Distinct().OrderByDescending(x => x.ww);
            // The controls have a Work Week Range so we need both a start and end control
            var DDLWWStartVM = new DropDownVM();
            DDLWWStartVM.Categories = new SelectList(DDLWWQuery, "ww", "ww");
            ViewBag.DDLWWStart = DDLWWStartVM;
            //ViewBag.DDLWWStart = new SelectList(DDLWWQuery.AsEnumerable(), "ww", "ww", "-- Select Starting WW --");

            //ViewBag.DDLWWEnd = new SelectList(DDLWWQuery.AsEnumerable(), "ww", "ww", "-- Select Ending WW --");

            var DDLWWEndVM = new DropDownVM();
            DDLWWEndVM.Categories = new SelectList(DDLWWQuery, "ww", "ww");
            ViewBag.DDLWWEnd = DDLWWEndVM;
        }
        return View();
    }

索引视图片段:

@model IEnumerable<TaskTracker.ViewModels.DropDownVM>


@{
    ViewBag.Title = "Create Indicator: Step One of Two";
}

<h2>Indicators</h2>

<p>This is currently being refined.</p>


<div>Create Indicator: Step One of Two - Select Work Week Range</div>

using (Html.BeginForm("StepTwo", "Indicator", new { wwStartSelect = WWStart.Categories.Text, wwEndSelect = WWEnd.Categories.Text }))
{
<div>Select Starting Work Week</div>

@Html.DropDownList("WWStart", (SelectList)ViewBag.DDLWWStart, " -- Select Starting Work Week -- ")

<div>Select Ending Work Week</div>

@Html.DropDownList("WWEnd", (SelectList)ViewBag.DDLWWEnd, " -- Select Ending Work Week -- ")

@Html.AntiForgeryToken()
<input type="submit" title="Next Step" value="Next Step" />
}

如何通过修复我的代码来解决此错误以获得所需的结果?

【问题讨论】:

  • 不用.Categories吗?

标签: c# asp.net asp.net-mvc razor asp.net-mvc-5


【解决方案1】:

您没有投射正确的对象。您应该将其转换为 Categories 属性...见下文

@Html.DropDownList("WWStart", (SelectList)ViewBag.DDLWWStart.Categories, " -- Select Starting Work Week -- ")
@Html.DropDownList("WWEnd", (SelectList)ViewBag.DDLWWEnd.Categories, " -- Select Ending Work Week -- ")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-06
    相关资源
    最近更新 更多