【问题标题】:Display two column values in Razor drop down list? or Html drop down list?在 Razor 下拉列表中显示两列值?还是 Html 下拉列表?
【发布时间】:2016-10-26 06:28:32
【问题描述】:

名称和 ID 存储在数据库中。我想将数据库中的所有名称和 ID 显示到 ViewPage Dropdown。在这里,我仅在下拉列表中显示名称列。是否可以在单个下拉列表中显示两列?请告诉我解决方案。

控制器:

var Employe = (from table in dc.tbl_EmployeeDetails
               select table.Employee_Name).ToList();
ViewData["empName"] = Employe ;

var department = (from table in dc.tbl_EmployeeDetails
                  select table.Department).ToList();
ViewData["dept"] = department;

查看

<select id="reportto" disabled="disabled" font-size:14px">
    @foreach(var person in ViewData["empName"] as List<string>)
    {
        if(@Model.employee!=@person)
        {
           <option>@person</option>
        }
    }
</select>

这里我只在下拉列表中显示名称。如何在单个下拉列表中同时显示姓名和部门?

【问题讨论】:

    标签: html asp.net-mvc razor


    【解决方案1】:

    请做一件事。

    请使用 SelectListItem 绑定下拉列表。

    List<SelectListItem> lstLocation = new List<SelectListItem>();
    
    SqlParameter[] parameters = { new SqlParameter("@ID", ID),new 
    SqlParameter("@Name",Name)};
    
    DataSet dsLocation = SqlHelper.ExecuteDataset(CommandType.StoredProcedure, "GetPersonalInfo", parameters);
    
       if (dsLocation != null && dsLocation.Tables.Count > 0 && dsLocation.Tables[0].Rows.Count > 0)
                {
                    lstLocation = (from drLocation in dsLocation.Tables[0].AsEnumerable()
                                   select new SelectListItem { Text = `drLocation["PersonID"].ToString() + ' - '+ drLocation["PersonName"].ToString(), Value = drLocation["PersonID"].ToString() }).ToList();`
                }
    

    并将此列表对象存储到 view bag 并设置为 Dropdownlist in view 。

    ViewBag.Location=lstLocation ;
    
    @Html.DropDownList("Location", ViewBag.Location as List<SelectListItem>,
     "-SELECT-", new { data_val = "true", data_val_required = "Please select 
     location", @class = "form-control" })
    

    使用上面的代码,您的数据将类似于下拉列表中的“1-ROnak”、“2-Manish”。 选择后,您可以使用'-'分割名称和ID。

    谢谢。

    【讨论】:

      【解决方案2】:

      在控制器中:

      ViewBag.EmployeeDetails= db.EmployeeDetails.ToList();

      在视图中:

      @model ...
      @{
              ViewBag.Title ="Employe ";
              List<Proyect.Models.EmployeeDetails> Details = ViewBag.EmployeeDetails;
      }
      
      <select name="EmployeeDetails" class="form-control" id="EmployeeDetails">
                  <option>Select one</option>
                  @foreach (var Detail  in Details)
                  {
                  <option id="@Docs.Id_Employee_Detail" value="@Docs.Id_Employee_Detail">@Detail.Name : @Detail.Department</option>
                  }
      </select>

      你在控制器中收到参数

      public ActionResult Something(int EmployeeDetails)
      {
      var EmployeeDetailSelected = dc.tbl_EmployeeDetails.Find(EmployeeDetails);
      }

      【讨论】:

        猜你喜欢
        • 2017-03-07
        • 1970-01-01
        • 2016-10-22
        • 1970-01-01
        • 2014-08-09
        • 2014-06-05
        • 1970-01-01
        • 2021-08-17
        • 2016-01-28
        相关资源
        最近更新 更多