我想知道如何从页面发布数据并在同一页面显示结果?我有一个具有以下代码的控制器:我创建了一个单独的视图“结果”,最终指向索引。处理结果并显示在同一页面asp.net mvc
//EmployeeController.cs
public ActionResult Index (List<Employee> employees = null)
{
employees = employees == null
? db.Employees.Include(e => e.Department).ToList()
: employees;
return View(employees);
}
public ActionResult Result(Employee employee, decimal minsal,decimal maxsal)
{
var employees = db.Employees.Include(e => e.Department);
employees = db.Employees
.Where(p => p.DepartmentID == employee.DepartmentID
&& p.Salary > minsal && p.Salary < maxsal);
var empList = employees.ToList();
ViewBag.DepartmentID = new SelectList(db.Departments, "DepartmentID", "DeptName", employee.DepartmentID);
return View("Index", empList);
}
查看:(Result.cshtml)
@model _4th_assignment.Models.Employee
@{
ViewBag.Title = "Result";
}
<h2>Result</h2>
@using (Html.BeginForm())
{
<p>
Select Department: @Html.DropDownList("DepartmentID", "--select--")
</p>
<p>
Enter Salary Range: Min @Html.TextBox("minsal") Max
@Html.TextBox("maxsal")
</p>
<p> <input type="submit" value="Filter" /></p>
}
我已经创建了索引页面的链接 “过滤器与工资范围和部门的职员”。当我点击这个链接时,它会进入结果页面,然后筛选结果显示在索引页面中。
查看:(index.cshtml)
@model IEnumerable<_4th_assignment.Models.Employee>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<p>
@Html.ActionLink("filter the employees with salary range and department ", "Result")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
@Html.DisplayNameFor(model => model.MiddleName)
</th>
<th>
@Html.DisplayNameFor(model => model.LastName)
</th>
<th>
@Html.DisplayNameFor(model => model.Salary)
</th>
<th>
@Html.DisplayNameFor(model => model.Department.DeptName)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.MiddleName)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Salary)
</td>
<td>
@Html.DisplayFor(modelItem => item.Department.DeptName)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.EmployeeID }) |
@Html.ActionLink("Details", "Details", new { id=item.EmployeeID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.EmployeeID })
</td>
</tr>
}
</table>
现在我想做的事情是,如果我点击链接在索引页它shuold要“过滤薪金范围和部门的职员”输入索引页中,结果也应该只显示在索引页中。