Asp.Net 中用于搜索的 MVC 中的值绑定

Value Binding In Asp.Net MVC for Searching

在 Asp.Net MVC 中,我在定义下面用于从员工 Table 搜索 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; }
}

和 Controller 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,我在使用时遇到问题。

首先,您的视图正在使用 name="empStatus" 创建一个 <select>,但您的方法有一个名为 employeeStatus 的参数。由于名称不匹配 employeeStatus 的值将是 null 并且永远不会执行 .Where() 语句。此外,@using (Html.BeginForm()) 的默认操作是 FormMethod.Post,因此您需要指定 FormMethod.Get。当您应该发布 int Id 值时,您还发布了 EmployeeStatusstring Status 属性。

首先创建一个表示您要在视图中显示的内容的视图模型

public class EmployeeListVM
{
  public string SearchText { get; set; }
  public int? Status { get; set; }
  public SelectList StatusList { get; set; }
  public IEnumerable<Employee> Employees { get; set; }
}

并在视图中

@model EmployeeListVM
@using Html.BeginForm("Index", "Employee", FormMethod.Get))
{
  @Html.TxtBoxFor(m => m.SearchText)
  @Html.DropDownListFor(m => m.Status, Model.StatusList, "All")
  <input type="submit" value="Search" />
}
@(foreach var employee in Model.Employees)
{
  .... // display employees
}

然后在控制器中

public ActionResult Index(string searchText, int? status)
{
  var statusList = db.EmployeeStatus;
  var employees = db.Employee;

  if (!String.IsNullOrEmpty(searchText))
  {
    employees = employees.Where(e => e.EmployeeName.Contains(searchText));
  }
  if (status.HasValue)
  {
    employees = employees.Where(e => e.EmployeeStatus == status.Value);
  }
  var model = new EmployeeListVM
  {
    SearchText = searchText,
    Status = status,
    StatusList = new SelectList(statusList, "Id ", "Status"),
    Employees = employees
  };
  return View(model);
}