views:

1356

answers:

5

I have forms in my page a get and a post and i want add pager on my get form .. so i cant page through the results..

The problem that i am having is when i move to the second page it does not display anything..

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

this my actions code.

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

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

and this is what i am using on my get form to page through

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

instead of going to this this

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

which sort of makes sence .. but i cant really think of any other way of doing this

Any help would be very appreciated..

Thanx

+1  A: 

Of course it will hit the GET version of SearchByAttraction because using this control you have a 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 hit the POST version, because clicking the link will submit your form on the server using POST request.

maxnk
A: 

but then my paging wouldnt be dyamic any more.

devforall
+1  A: 

Try this:

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

id should contain the page number you need to display.

If you lose form values using this approach then you'll need to change the Html.Pager method to render each action link as a form submit link.

Todd Smith
+1  A: 

Thanx everyone i finally got it working .. just used one form .. 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 search method every time it does a post..

devforall
+7  A: 

I'd recommend against doing paging via HTTP POST. Page and search criteria are 2 perfect examples of what querystrings are meant for. Put those values in the query string & have that load up your action args.

Think about this. You can search google for "pies", navigate to page 14, copy the link and send it to your grandma. You can't do that when your paging/search only works with form posts.

Ben Scheirman
Hm... Good point :)
maxnk