降DownList搜索

编程入门 行业动态 更新时间:2024-10-10 12:20:54
本文介绍了降DownList搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在下面定义的索引方法搜索的employeeStatus的列表从员工表使用Asp.Net MVC3,我有问题。和的employeeStatus型号为:

Using Asp.Net MVC3, I am having problem in defining the Index Method below for searching the list of EmployeeStatus From Employee Table. and EmployeeStatus Model as:

public class EmployeeStatus { public int Id { get; set; } public string Status { get; set; } }

我已经创建Employee.cs型号如下:

I have created Employee.cs Model as follows:

public class Employee { public int ID { get; set; } public string EmployeeName { get; set; } public int EmployeeStatus { get; set; } }

和控制器EmployeeController用行动指数

and Controller EmployeeController with action Index

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是:

and my DBScript is:

CREATE TABLE雇员(ID INT PRIMARY KEY IDENTITY(1,1),EmployeeName为nvarchar(255),INT的employeeStatus)INSERT INTO员工VALUES('阿肖克',0)INSERT INTO员工VALUES('阿希什',1)INSERT INTO员工VALUES('Ashik',2)CREATE TABLE的employeeStatus(ID INT,状态为nvarchar(100))INSERT INTO的employeeStatus值(0,活动)INSERT INTO的employeeStatus值(1,无效)INSERT INTO的employeeStatus值(3,'短期')

我如何从下拉菜单中输入搜索的employeeStatus,我有问题,如何使用它。

How Do I Search from Dropdown List for the EmployeeStatus, I am having problem how to use it.

推荐答案

首先你的看法是创建一个&LT;选择&GT; 与 NAME = empStatus,但你的方法有一个名为参数的employeeStatus 。由于名称不匹配的employeeStatus 的值将是空和。凡() 语句将永远不会被执行。此外, @using(Html.BeginForm())是 FormMethod.Post ,所以你需要的默认操作指定 FormMethod.Get 。您还回发的employeeStatus 的字符串状态属性时,你应该张贴 INT ID 值。

Firstly your view is creating a <select> with name="empStatus" but you method has a parameter named employeeStatus. Since the names do not match the value of employeeStatus will be null and .Where() statement will never be executed. In addition, the default action for @using (Html.BeginForm()) is FormMethod.Post so you need to specify FormMethod.Get. You are also posting back the string Status property of EmployeeStatus when you should be posting the int Id value.

通过创建一个视图模型重新presenting要在视图中显示哪些启动

Start by creating a view model representing what you want to display in the view

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

更多推荐

降DownList搜索

本文发布于:2023-11-30 12:50:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650034.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:DownList

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!