2010-10-28 27 views
1

我有一个页面,我有一个控制网格的下拉列表。对于这个网格我使用MVCContrib。 当我改变我的dropdownlist的值,控制器的HttpPost方法被调用,这工作正常。 但是,当我页面或排序网格时,始终调用HttpGet方法。为什么?这当然不是它应该如何? 现在在下面的代码中我考虑到了这一点,我有一些工作。但是代码是很好的,特别是Session的使用。必须有更好的方式来处理这个问题? 控制器代码是;MVCContrib网格:分页和排序执行控制器的HttpGet方法。为什么?

[HttpGet] 
[Authorize(Roles = "Administrator, AdminAccounts, ManagerAccounts")] 
public ActionResult ListHistory(GridSortOptions sort, int? page) 
{ 
    EmployeeListViewModel employees = null; 
    if (sort.Column == null && page.HasValue == false) 
    { 
     employees = new EmployeeListViewModel(EmployeeExtended.GetAllFormerEmployees(), 1); 
     Session["EmployeeListViewModel"] = employees; 
    } 
    else 
    { 
     employees = Session["EmployeeListViewModel"] as EmployeeListViewModel; 
    } 

    if (sort.Column != null) 
    { 
     employees.EmployeeList = employees.EmployeeList.OrderBy(sort.Column, sort.Direction); 
    } 
    int pageLength = Convert.ToInt32(ConfigurationManager.AppSettings["EmployeeListPageLength"].ToString()); 
    employees.EmployeeList = employees.EmployeeList.AsPagination(page ?? 1, pageLength); 
    ViewData["sort"] = new GridSortOptions(); 
    return View(employees); 
} 

[HttpPost] 
[Authorize(Roles = "Administrator, AdminAccounts, ManagerAccounts")] 
public ActionResult ListHistory(GridSortOptions sort, EmployeeListViewModel elvm, int? page) 
{ 
    IEnumerable<EmployeeExtended> employees = null; 

    switch (elvm.OptionsId) 
    { 
     case 1: employees = EmployeeExtended.GetAllFormerEmployees(); 
      break; 
     case 2: employees = EmployeeExtended.GetAllOnNoticeEmployees(); 
      break; 
     case 3: employees = EmployeeExtended.GetAllCurrentEmployees(); 
      break; 
    } 

    if (sort.Column != null) 
    { 
     employees = employees.OrderBy(sort.Column, sort.Direction); 
    } 
    int pageLength = Convert.ToInt32(ConfigurationManager.AppSettings["EmployeeListPageLength"].ToString()); 
    employees = employees.AsPagination(page ?? 1, pageLength); 
    ViewData["sort"] = sort; 
    EmployeeListViewModel elvm1 = new EmployeeListViewModel(employees, elvm.OptionsId); 
    Session["EmployeeListViewModel"] = elvm1; 
    return View(elvm1); 
} 

查看代码是;

<% using (Html.BeginForm()) 
    {%> 
    <%: Html.AntiForgeryToken() %> 
<fieldset> 
    <legend>List of Employees</legend> 
    <% if (ViewData["LastPersonMessage"] != null && ViewData["LastPersonMessage"].ToString().Length > 0) 
     { %> 
     <p class="error"> 
      At <% Response.Write(DateTime.Now.ToString("T")); %>. <%: ViewData["LastPersonMessage"]%>. 
     </p> 
    <%} %> 
    <p>Click on history to view the history of an employee.</p> 
    <p>Select which employees you want to see: 
     <%:Html.DropDownListFor(model => model.OptionsId, Model.Options, new { onchange = "this.form.submit();" })%> 
    </p> 
    <%: Html.Grid(Model.EmployeeList).Columns(column => 
    { 
     column.For(model => Html.ActionLink("History", "ShowHistory", new { employeeId = model.EmployeeId })).Named("").DoNotEncode(); 
     column.For(model => model.Forename); 
     column.For(model => model.Surname); 
     column.For(model => model.DivisionName); 
     column.For(model => model.DepartmentName); 
     column.For(model => model.StartDate).Format("{0:d}"); 
     column.For(model => model.EndDate).Format("{0:d}"); 
    }).Sort((GridSortOptions)ViewData["sort"])%> 
    <p><%= Html.Pager((IPagination)Model.EmployeeList)%></p> 
    <p></p> 
</fieldset> 
<% } %> 

回答

0

我没有经历过你的代码,但从概念上讲,你观察到的是它应该如何。即:

如果您的操作无意更改您的应用程序中的数据,那么使用GET就是正确的Http动词。

POST是操作更改数据时的正确动词。因为你只是改变你的数据页面,或者只是对它进行排序,那么数据本身不会改变,只是显示,所以一个帖子会是错误的。

所以,随着歌曲的发展,你“一定做得对”。

相关问题