处理结果并显示在同一页面asp.net mvc中

时间:2013-09-25 14:34:05

标签: c# asp.net-mvc razor

我想知道如何从页面发布数据并在同一页面中显示结果?我有一个带有以下代码的控制器:我创建了一个单独的视图“Result”,最终指向Index。

//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会询问索引页面中的输入,结果也应该显示在索引中仅限页面。

2 个答案:

答案 0 :(得分:0)

这样做的一种方法是, 从Result操作返回partialview()而不是view()。 制作result.cshtml,一个部分视图。 把这个partialview放在index.cshtml

答案 1 :(得分:0)

您需要大约4次修改。

  1. 专门为结果视图创建_IndexPartial.cshtml部分视图,除非您确实需要使用布局的索引视图? (与index.cshtml w / out布局相同)
  2. 创建一个ActionResult,返回带有IEnumerable<_4th_assignment.Models.Employee列表的部分视图。

    public ActionResult GetEmployees(int deptId)
    {
        var employees = from a in db.Employees
                       select a;
        employees = employees.Where(a => a.DeptId.Equals(deptId));
    
        //... code here
    
        return PartialView("_IndexPartial", employees); 
    }
    
  3. 在结果视图中创建一个空div

    ie:<div id="employee-list"></div>

  4. Html.BeginForm 更改为 Ajax.BeginForm ,如下所示:

    @using(Ajax.BeginForm(“GetEmployees”,“Controller”,new AjaxOptions()  {

    HttpMethod = "POST",
    UpdateTargetId = "employee-list",
    InsertionMode = InsertionMode.Replace
    

    }))

  5. 注意:UpdateTargetId是一个AJAX回调

相关问题