C# 如何在不使用实体框架的情况下以编辑模式ASP.NET MVC(C)填充dropdownlist

C# 如何在不使用实体框架的情况下以编辑模式ASP.NET MVC(C)填充dropdownlist,c#,asp.net-mvc,drop-down-menu,C#,Asp.net Mvc,Drop Down Menu,我不喜欢使用实体框架,所以我想在这里编辑时实现这一点 我想显示dropdownlist的部门值,当用户选择下拉列表时,它将从数据库加载所有列表 以下是我在编辑中的控制器代码: //GET data from database public ActionResult UpdatePos(int pid) { DepartmentList posmodel = new DepartmentList(); DataTable dt = new DataT

我不喜欢使用实体框架,所以我想在这里编辑时实现这一点

我想显示dropdownlist的部门值,当用户选择下拉列表时,它将从数据库加载所有列表

以下是我在编辑中的控制器代码:

//GET data from database
public ActionResult UpdatePos(int pid)
{
            DepartmentList posmodel = new DepartmentList();
            DataTable dt = new DataTable();

            using (MySqlConnection mysqlcon = new MySqlConnection(conn))
            {
                mysqlcon.Open();
                string qry = "SELECT id,position,department FROM positions WHERE id = @pid";
                MySqlDataAdapter da = new MySqlDataAdapter(qry, mysqlcon);
                da.SelectCommand.Parameters.AddWithValue("@pid", pid);
                da.Fill(dt);
            }

            if (dt.Rows.Count == 1)
            {
                //id and posistion is showing in my view
                posmodel.Model.pid = Convert.ToInt32(dt.Rows[0][0].ToString());
                posmodel.Model.position = dt.Rows[0][1].ToString();
                posmodel.dplist = """"I don't know what should I code here""""
                return View(posmodel);
            }
            else
                return RedirectToAction("Index");
}
以下是我的看法:

<div class="form-group">
            @Html.LabelFor(model => model.Model.position, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Model.position, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Model.position, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.dplist, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.dplist, new SelectList(Model.dplist, "deptname", "deptname",Model.Model.did))
                @Html.ValidationMessageFor(model => model.dplist, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Model.pid, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Model.pid, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Model.pid, "", new { @class = "text-danger" })
            </div>
        </div>
我的模型是

 public class PositionModel
    {
        [Display(Name = "Position")]
        [Required(ErrorMessage = "Position required.")]
        public string position { get; set; }
        [Display(Name = "Department")]
        public string dept { get; set; }
        public int pid { get; set; }

        //public List<DeptList> dplist { get; set; }
        public DeptList DeptList { get; set; }
        public int did { get; set; }
        public string deptname { get; set; }

    }

    public class DeptList
    {
        public int did { get; set; }
        public string deptname { get; set; }
    }

你为什么不使用实体框架呢?asp.net mvc对我来说是新的,我觉得这种方式更适合我,因为我来自vb.net,这种编码风格对我来说很容易理解。但我应该尝试使用实体框架吗?是的,使用实体框架。这对你的简历来说更好,而且这种方式是老式的技术。不,先生,顺便说一句,departname是我想在dropdownlist posmodel.dplist=new SelectListdt.AsDataView,deptname,deptname;错误=无法将类型system.web.mvc.selectlist隐式转换为system.collection.genericlist。谢谢你的回复,我很感激,先生。顺便说一句,先生,我更新了我的问题,我把输出图像。thanksok cool,更改posmodel.dplist=new SelectListdt.AsDataView,deptname,deptname;您需要查看是否可以将dt.AsDataView转换为列表,即try.tolist谢谢您的回复。我尝试这些代码时出现了相同的错误-posmodel.dplist=new SelectListdt.AsDataView.ToList,deptname,deptname;-posmodel.dplist=新建SelectListdt.AsDataView、deptname、deptname.ToList;-posmodel.dplist=新建SelectListdt.AsEnumerable、deptname、deptname.ToList;如果你喜欢我的答案,请把它标出来。EF方面的专家,因此可以提供帮助。
  posmodel.dplist = new SelectList(dt.AsDataView(), "pid", "position");