tags:

views:

14

answers:

1

I have a page where I have a dropdownlist which controls a grid. For this grid I use MVCContrib. When I change the value of my dropdownlist, the Controller's HttpPost method is called and this works fine. However when I page or sort the grid, the HttpGet method is called, always. Why? This is surely not how it should be? Now in the code below I take this into account and I have something that works. But the code is aweful, especially the use of Session. There must be a better way of dealing with this? The controller code is;

[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);
}

The view code is;

<% 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>
<% } %>
A: 

I haven't gone through your code, but conceptually, what you have observed is how it should be. Ie:

If your action is not intended to change the data in your app, then using a GET is the correct Http verb.

POST is the correct verb when your action changes the data.

As you are just changing the page of your data, or just sorting it, then the data itself does not change, just the display, so a post would be wrong.

So, as the song goes, you "must have done something right".

awrigley