问题
In Asp.Net MVC, I have 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; }
}
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; }
}
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));
}
and 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" />
}
and my DBScript is:
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')
How Do I Search from Dropdown for the EmployeeStatus, I am having problem how to use it.
回答1:
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.
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; }
}
and in the view
@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
}
Then in the controller
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);
}
来源:https://stackoverflow.com/questions/32999277/value-binding-in-asp-net-mvc-for-searching