【问题标题】:Value Binding In Asp.Net MVC for SearchingAsp.Net MVC 中用于搜索的值绑定
【发布时间】: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


【解决方案1】:

首先,您的视图使用name="empStatus" 创建一个&lt;select&gt;,但您的方法有一个名为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);
}

【讨论】:

    猜你喜欢
    • 2010-09-27
    • 2015-12-19
    • 1970-01-01
    • 2012-09-03
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2016-02-25
    • 1970-01-01
    相关资源
    最近更新 更多