我尝试使用以下代码使用Html Helper(Html.DropdownList)在MVC中创建下拉列表,但该列表未在UI上呈现。我试图将DropDownList绑定到一个IEnumerable的SelectListItems集合,但没有得到确切的结果。有人可以指出缺失的链接。以下是代码的fiddler https://dotnetfiddle.net/26h7xa
,下面是代码:
项目模型:
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 :(得分: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中的下拉列表的最佳做法,请参阅以下链接。
答案 1 :(得分:0)
使用ViewBag
ViewBag.Employee = employee;
并在您的视图中将DropdownList更改为:
@Html.DropDownList("employee", new SelectList(ViewBag.Employee, "Id", "FirstName"))