2017-08-09 121 views
-1

我想使用下面的代碼在Html Helper(Html.DropdownList)中創建MVC下拉列表,但列表沒有得到呈現在UI上。我試圖綁定DropDownList與SelectListItems的IEnumerable集合,但沒有得到確切的結果。有人可以指出缺失的鏈接。下面是該代碼的提琴手https://dotnetfiddle.net/26h7xa以下是代碼:Html幫助器下拉列表不會呈現下拉列表中的項目列表

該項目的模型:

using System; 
using System.ComponentModel.DataAnnotations; 

namespace HelloWorldMvcApp 
{ 
    public class Employee 
    { 
     public Guid Id { get; set; } 

     public string FirstName { get; set; } 

     public string LastName { get; set; } 

     public string Salary { get; set; } 

     public string Designation { get; set; } 

     public DateTime DateOfJoining { get; set; } 

     public bool Status { get; set; } 
    }  
} 

控制器:

using System; 
using System.Web.Mvc; 
using System.Collections.Generic; 

namespace HelloWorldMvcApp 
{ 
    public class HomeController : Controller 
    { 
     [HttpGet] 
     public ActionResult Index() 
     { 
      return View(EmployeeData()); 
     } 


     public IEnumerable<Employee> EmployeeData() 
     { 
      IEnumerable<Employee> employee = new List<Employee> 
      { 
       new Employee { Id = Guid.NewGuid(), FirstName = "Sahil", LastName = "Sharma", Designation = "Software Engineer", Salary = "1000", DateOfJoining = DateTime.Now, Status =true }, 
       new Employee { Id = Guid.NewGuid(), FirstName = "Sahil", LastName = "Sharma", Designation = "Software Engineer", Salary = "1000", DateOfJoining = DateTime.Now, Status = false }, 
       new Employee { Id = Guid.NewGuid(), FirstName = "Sahil", LastName = "Sharma", Designation = "Software Engineer", Salary = "1000", DateOfJoining = DateTime.Now, Status =true }, 
       new Employee { Id = Guid.NewGuid(), FirstName = "Sahil", LastName = "Sharma", Designation = "Software Engineer", Salary = "1000", DateOfJoining = DateTime.Now, Status =true }, 
       new Employee { Id = Guid.NewGuid(), FirstName = "Sahil", LastName = "Sharma", Designation = "Software Engineer", Salary = "1000", DateOfJoining = DateTime.Now, Status =true }, 
      }; 
      return employee; 
     } 
    } 
} 

查看:

@using HelloWorldMvcApp 
@model IEnumerable<Employee> 

@{ Layout = null; } 

<!DOCTYPE html> 

<html lang="en"> 
<head> 
    <title>Html Dropdown List</title> 
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css"> 
</head> 

<body> 
    <div class="container"> 
     <div class="form-inline"> 
      <div class="col-md-12 col-lg-12"> 
       <div class="col-md-4 col-lg-4"> 
        <label class=""> Select Employee: </label> 
       </div> 

       <div class="col-md-8 col-lg-8"> 
        @{ 
         var employee = Model; 
         IEnumerable<SelectListItem> list = from e in employee select new SelectListItem { Selected = true, Text = Convert.ToString(e.Id), Value = String.Concat(e.FirstName, " ", e.LastName) }; 

         Html.DropDownList("ddlEmployee", new SelectList(list)); 
        } 
       </div> 
      </div> 
     </div> 
    </div> 

    <!-- JS includes --> 
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
    <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script> 

</body> 
</html> 

是有一些我在Html Helper中錯過了,因爲我已經找過各種各樣的文章了使用相同的方法綁定DropdownList。任何建議將受到歡迎。

+0

@StephenMuecke不需要'@'becaouse其內部'@ {...}' –

+0

@IrshadJm是的,有! –

+0

不需要在IEnumerable 上使用''新選擇列表'' - 只需使用'@ Html.DropDownList(「ddlEmployee」,list)'。同樣,你的'SelectListItem'中的'Text'和'Value'內容是相反的(GUID通常被用作返回給控制器的值)。 –

回答

5

在模型中加入一個財產等作爲,

public List<SelectListItem> lstEmployee {get; set;} 

在你的控制器的動作

public ActionResult Index() 
{ 
    Employee model = new model(); 
    IEnumerable<Employee> empList = EmployeeData(); 
    model.lstEmployee = from e in empList select new SelectListItem { Selected = true, Text = Convert.ToString(e.Id), Value = String.Concat(e.FirstName, " ", e.LastName) }; 
    return View(); 
} 

在您看來,

@model Employee // Whatever your model path 

@Html.DropDownList("ddlEmployee", model.lstEmployee) 

還爲最佳實踐在MVC綁定下拉列表中,請參考以下鏈接。

Link 1

Link 2

0

使用ViewBag

在控制器

ViewBag.Employee = employee; 

,並在您的視圖改變DROPDOWNLIST爲:

@Html.DropDownList("employee", new SelectList(ViewBag.Employee, "Id", "FirstName"))