无法使用 mvc 检索模型中的 DropdownList 值 & ado.net

Can not retrieve DropdownList value in model using mvc & ado.net

我正在发送选定列表以使用 ViewBag 查看。这是我通过 ViewBag

传递的 get 方法
public List<Dept> GetDept()
 {
    connection();
    List<Dept> deptList = new List<Dept>();

    SqlCommand com = new SqlCommand("Sp_GetDept", con);
    com.CommandType = CommandType.StoredProcedure;
    SqlDataAdapter da = new SqlDataAdapter(com);
    DataTable dt = new DataTable();

    con.Open();
    da.Fill(dt);
    con.Close();
    //Bind EmpModel generic list using dataRow     
    foreach (DataRow dr in dt.Rows)
    {
        deptList.Add(
            new Dept
            {
                DeptId = Convert.ToInt32(dr["DeptId"]),
                Name = Convert.ToString(dr["Name"])
            }
       );
    }
    return deptList;
}

public ActionResult Create()
{
    DeptRepo repo = new DeptRepo();
    ViewBag.Dept = new SelectList(repo.GetDept(), "DeptId", "Name");
    return View();
}

查看代码:

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

学生模型:

public class Student
{
    public int StudentId { get; set; }

    public string Name { get; set; }

    public string Roll { get; set; }

    public int DeptId { get; set; }

    public virtual Dept Dept { get; set; }
}

post方法:

[HttpPost]
    public ActionResult Create(Student std)
    {
        try
        {
            if (ModelState.IsValid)
            {
                StudentRepo repo = new StudentRepo();

                repo.AddStudent(std);
            }

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

在 post 方法中,在学生对象中发现下拉列表 ID 值为空。 谁能告诉我如何使用 mvc 和 ado.net 检索外键 ID。 任何形式的帮助将不胜感激。

您当前的代码,

@Html.DropDownList("Dept", null, "--Select--", 
                                 htmlAttributes: new { @class = "form-control" })

将为名称属性值设置为 Dept

的 SELECT 元素生成 HTML 标记
<select class="form-control" id="Dept" name="Dept">
   <option value="">--Select--</option>
</select>

由于您使用 Student class 作为您的 httppost 操作方法参数,模型绑定正确地将 selected 选项值映射到 DeptId [= Student 对象的 47=],您需要确保您的 select 元素名称也是 DeptId

如果您的视图是 Student class 的强类型,您可以使用 DropDownListFor 辅助方法

@Html.DropDownListFor(a => a.DeptId, ViewBag.Dept as IEnumerable<SelectListItem>, 
                           "--Select--", htmlAttributes: new { @class = "form-control" })

您可以使用 DropDownList 方法并将 DeptId 作为第一个参数(控件名称)并明确指定用于构建选项的集合作为第二个参数。

@Html.DropDownList("DeptId", ViewBag.Dept as IEnumerable<SelectListItem>, 
                    "--Select--", htmlAttributes: new { @class = "form-control" })

这将呈现名称属性值设置为 DeptId 的 SELECT 元素,当提交表单时,模型绑定器将能够使用 selected 选项值来将其设置为 Student 对象的 DeptId 属性(这是您的 httppost 操作方法参数)