Asp中的值绑定Net MVC搜索



. Net MVC,我在定义下面的索引方法从雇员表中搜索EmployeeStatus列表时遇到了问题。和

EmployeeStatus Model as:

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));
        }

和View as:

 @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);
}

最新更新