在数据库中插入下拉文本

时间:2016-09-28 10:42:01

标签: asp.net asp.net-mvc-5 asp.net-mvc-5.2

我创建了一个注册页面。

这是我在数据库中注册的表:

enter image description here

这是我的教育表:

enter image description here

我按照该值填写教育表格和fil下拉列表。

dropdownlist的代码:

    public static class Education
{
    public static IEnumerable<SelectListItem> GetEducationList()
    {
        using (AllameHelliDB db = new AllameHelliDB())
        {
            var List = (from a in db.Tbl_Educations
                        orderby a.EduID
                        select new SelectListItem { Text = a.EduName, Value = a.EduID.ToString() });
            return List.ToList();
        }

    }
}

    @Html.LabelFor(model => model.MotherEducation)
    @Html.DropDownListFor(model=>model.Paye,Allame_Helli.Models.Education.GetEducationList(), 
new { @class = "form-control" })

我需要在EduName中添加MotherEducation表格中添加tabel Regsiter。当我插入数据时返回null。

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

你的drop自己被绑定到Paye字段,所以这是获取EduID的字段,如果你想获得所选选项的文本EduName,那么你可以从Action本身做到这一点,通过阅读Paye属性并使用此ID检索Education对象,然后填写MotherEducation。

或者您可以为MotherEducation字段添加另一个下拉列表,但您必须同时创建Text和Value = EduName