在DropDownListFor中选择后如何从SelectList中获取值?

时间:2019-01-24 19:49:26

标签: c# asp.net

当在DropDownListFor中选择一个值时,当前我获得enc_manager_empid / emp_managerName选择的值。 选择员工姓名后,我还想获得emp_email的值。 我怎样才能做到这一点?

控制器:

 private void BindEmployees()
        {
            var employees = db.employyetable
             .Select(x => new {
                 FullName = x.emp_namefirst + " " + x.emp_namelast,
                 emp_email = x.emp_email,
                 Id = x.emp_id
             })
             .ToList();

            ViewBag.requestors = new SelectList(employees, "Id", "FullName","emp_email");          
        }

查看:

@Html.HiddenFor(model => model.enc_manager_empid, new { htmlAttributes = new { @class = "form-control" } })
@Html.HiddenFor(model => model.emp_managerName, new { htmlAttributes = new { @class = "form-control" } })

@Html.DropDownListFor(model => model.emp_manager, (IEnumerable<SelectListItem>)ViewBag.requestors, "Select", new {  onchange="AddRequestorValues()" })
@Html.ValidationMessageFor(model => model.emp_managerName, "", new { @class = "text-danger" })


<script>
        function AddRequestorValues() {
            var emp_managerSelectedValue = $('#emp_manager option:selected').val();
            var emp_managerSelectedtext = $('#emp_manager option:selected').text();
            $("#enc_manager_empid").val(emp_managerSelectedValue);
            $("#emp_managerName").val(emp_managerSelectedtext);

}
</script>

0 个答案:

没有答案