2013-09-25 234 views
0

我想知道如何從頁面發佈數據並在同一頁面顯示結果?我有一個具有以下代碼的控制器:我創建了一個單獨的視圖「結果」,最終指向索引。處理結果並顯示在同一頁面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要「過濾薪金範圍和部門的職員」輸入索引頁中,結果也應該只顯示在索引頁中。

回答

0

這樣做的一種方法是, 從Result操作返回partialview()而不是view()。 使result.cshtml,partialview。 在index.cshtml中放入了此部分視圖

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

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

  4. 變化Html.BeginFormAjax.BeginForm這樣的:

    @using( Ajax.BeginForm(「GetEmployees」,「Controller」,new AjaxOptions() {

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

    }))

注:UpdateTargetId是一個AJAX回調

相關問題