【发布时间】:2015-10-07 17:59:00
【问题描述】:
在 Asp.Net MVC 中,我在定义下面的索引方法以从员工表中搜索 EmployeeStatus 列表时遇到问题。 和
EmployeeStatus 模型为:
public class EmployeeStatus
{
public int Id { get; set; }
public string Status { get; set; }
}
我创建了 Employee.cs 模型如下:
public class Employee
{
public int ID { get; set; }
public string EmployeeName { get; set; }
public int EmployeeStatus { get; set; }
}
和带有动作索引的控制器EmployeeController
public ActionResult Index(string searchString, string employeeStatus)
{
var StatusLst = new List<string>();
var StatusQry = from st in db.EmployeeStatus
orderby st.Status
select st.Status;
StatusLst.AddRange(StatusQry.Distinct());
ViewBag.empStatus = new SelectList(StatusLst);
var employee = from m in db.Employee
select m;
if (!String.IsNullOrEmpty(searchString))
{
employee = employee.Where(s => s.EmployeeName.Contains(searchString));
}
if (String.IsNullOrEmpty(employeeStatus))
return View(employee);
else
{
if (employeeStatus == "Active")
return View(employee.Where(st => st.EmployeeStatus == 0));
else if (employeeStatus == "Inactive")
return View(employee.Where(st => st.EmployeeStatus == 1));
else
return View(employee.Where(st => st.EmployeeStatus == 2));
}
并查看为:
@using (Html.BeginForm())
{
<
@Html.DropDownList("empStatus", "All")
</div>
<div class="span7">
@Html.TextBox("SearchString",null)
</div>
</div>
</div>
<input type="submit" class="btn primary" value="Search" />
}
我的 DBScript 是:
CREATE TABLE Employee(ID INT PRIMARY KEY IDENTITY(1,1),EmployeeName nvarchar(255), EmployeeStatus int)
INSERT INTO Employee VALUES ('Ashok',0)
INSERT INTO Employee VALUES ('Ashish',1)
INSERT INTO Employee VALUES ('Ashik',2)
CREATE TABLE EmployeeStatus(Id int,Status nvarchar(100))
INSERT INTO EmployeeStatus Values (0,'Active')
INSERT INTO EmployeeStatus Values (1,'InActive')
INSERT INTO EmployeeStatus Values (3,'ShortTerm')
如何从下拉列表中搜索 EmployeeStatus,我遇到了如何使用它的问题。
【问题讨论】:
-
“从 DropDownList 搜索”是什么意思?
-
@yohanis 使用 viewbag 从下拉列表中搜索。
-
请解释一下从下拉列表中搜索是什么意思?它是可搜索的下拉菜单吗?
标签: asp.net asp.net-mvc sql-server-2008 asp.net-mvc-3 html.dropdownlistfor