ASP.NET MVC and Paging - Search and Results Script

I have forms on my page and I receive a message and I want to add a pager to my form. Therefore, I cannot post the results.

The problem I am running into is when I go to the second page which does not show anything.

I am using this library for swap. http://stephenwalther.com/Blog/archive/2008/09/18/asp-net-mvc-tip-44-create-a-pager-html-helper.aspx

this code of my actions.

    [AcceptVerbs("GET")]
    public ActionResult SearchByAttraction()
    {
        return View();
    }  

    [AcceptVerbs("POST")]
    public ActionResult SearchByAttraction(int? id, FormCollection form)
    {....
    }

      

and this is what I use in my form to navigate to the page via

<% = Html.Pager (ViewData.Model)%> // but when I do it it goes to This method [AcceptVerbs ("GET")] public ActionResult SearchByAttraction ()

instead in this

[AcceptVerbs ("POST")] public ActionResult SearchByAttraction (int? Id, FormCollection form)

what kind of does, but I can't think of another way of doing it

Any help would be much appreciated.

Thanx

+1


source to share


4 answers


I would recommend not paging over HTTP POST. Page and search criteria are 2 great examples of what words are for. Put these values ​​in a query string and load your action arguments.



Think about it. You can google search for "pies", go to page 14, copy the link and send it to your grandma. You cannot do this when your paging / search only works with form messages.

+9


source


Of course it will end up in the GET version of SearchByAttraction because with this control you have links as output.

So what you need to do:



1. make form on the page:
    <form id="myForm" action="your/url" method="post">
        <input type="hidden" name="page" />

        <input type="hidden" name="your_param1" />
        <input type="hidden" name="your_param2" />
        <input type="hidden" name="your_paramN" />
    </form>

2. make changes to pager - it should produce something like that:

    <ul id="pager">
        <li><a href="url/as/was/created/by/pager" onclick="return submitMyForm(1);">1</a></li>
        <li><a href="url/as/was/created/by/pager" onclick="return submitMyForm(2);">2</a></li>
        <li><a href="url/as/was/created/by/pager" onclick="return submitMyForm(3);">3</a></li>
    </ul>

3. add simple javascript function on the page:

    <script language="javascript" type="text/javascript">
        function submitMyForm(page) {
            var form = document.forms["myForm"];
            form.elements["page"].value = page;
            form.submit();
            return false;
        }
    </script>

      

And you will be able to get into the POST version because clicking on the link will submit your form to the server with a POST request.

+1


source


Try the following:

[AcceptVerbs("GET")]
public ActionResult SearchByAttraction(int? id)
{
    return View();
}  

      

id should contain the page number you want to display.

If you lose form values ​​with this approach, you will need to change the Html.Pager method to display each action link as a form submit link.

+1


source


Thanx all i finally got it working .. just used one shape .. and did something like this

Controller actions

    [AcceptVerbs("GET")]
    public ActionResult SearchByAttraction()
    {
        return View();
    }

    public ActionResult Search(FormCollection form,int? id)
    {
        var info = _repository.ListByLocation(city, postal, pageIndex, 2);
        return View("SearchByAttraction", info); 
    }

      

View

<% using (Html.BeginForm("Search", "Home", FormMethod.Get))
{ %>

      

so it calls the lookup method every time it makes a post.

+1


source







All Articles