使用sql绑定下拉列表 [英] Binding drop down with sql

查看:99
本文介绍了使用sql绑定下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以查看任何人的记录,然后可以编辑它,如果我点击记录下面的编辑按钮,点击它后带你到另一个页面显示文本框和下拉列表,复选框编辑和所有文本框包含该EmpID的值,但问题是,我想要在DROPDOWN LIST中预先选择的值,就像它应该为每个用户选择相应的值,就像我的文本框一样。我正在使用LINQ TO SQL,MVC 3,asp.net C#



代码:



i can view anybody record and then can edit it if i click edit button below record, after clicking it takes you to another page whihc shows textboxes and dropdown, checkboxes to edit and all textboxes contains values for that EmpID but problem is that i want pre selected value in DROPDOWN LIST, like it should pick corresponding value for each user a preselected like my textboxes. I am using LINQ TO SQL, MVC 3, asp.net C#

CODE:

@using EmployeeAttendance_app.Models
@model IEnumerable<GetEmployeeEditDetails_SpResult>
          
@{
    var Item = Model.FirstOrDefault();
 }
           
<style type="text/css">

</style>
<div>
@using (Html.BeginForm("EditEmployee", "Home", FormMethod.Get))
{
  <label id="lblName" class="editEmp_label">Name</label>
  <input type="text" value= @Item.EmplName  name="EmpName" placeholder="Update Name" />
  <br />
  <label id="lblDept" class="editEmp_label">Department</label>
  @Html.DropDownList("DeptID", @Item.DeptName)
  <br />
  <label id="lblShift" class="editEmp_label">Shift</label>
  @Html.DropDownList("ShiftId")





控制器:



Controller:

public ActionResult EditEmployee() 
        {
            if (!String.IsNullOrEmpty(Session["Admin"] as string))
            {
                int? EmplId = Convert.ToInt32(Session["EmpEdit"]);
                IEnumerable<GetEmployeeEditDetails_SpResult> EmployeeValues = DataContext.GetEmployeeEditDetails_Sp(EmplId).ToList();
                var DepNames = (from n in DataContext.HrDepts select new { n.DeptID, n.DeptName }).Distinct();
                ViewData["DeptID"] = new SelectList(DepNames, "DeptID", "DeptName");
                var ShiftNames = (from n in DataContext.AtdShifts select new { n.ShiftId, n.ShiftName }).Distinct();
                ViewData["ShiftId"] = new SelectList(ShiftNames, "ShiftId", "ShiftName");
                return View(EmployeeValues);
            }

推荐答案

这篇文章可能会对你有帮助。

选择列表头痛的刺激因素

Joey
This post might help you.
Aspirin for Select List Headaches
Joey


这篇关于使用sql绑定下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆