Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC: calling a controller method from view

Tags:

asp.net-mvc

I'm implementing paging on an ASP.NET MVC view, and I want to call a method in the controller from the view.

Code in the view:

<a href="<%= Url.Action("Search", 
            new { page = NextPage(Request["exactPage"])).ToString()}) %>"> 

Controller method:

public string NextPage(string currentPage)
{
     return (int.Parse(currentPage) +  1).ToString();
}

How can I call the NextPage method from the view?

thanks!

like image 504
Alice in wonderland Avatar asked Oct 14 '22 08:10

Alice in wonderland


1 Answers

If you know the number of the current page, could you not just render links where the page value = current page plus/minus 1 for the previous and next links? You don't really need the view to ask the controller what the next/previous page number is. That information can be derived by the view based on the value of the current page index.

EDIT: I suggest that the controller passes an additional value to the view indicating the total number of pages available. The view can then compare this value against the current page number to determine whether to display the next link.

like image 58
pmarflee Avatar answered Oct 27 '22 00:10

pmarflee