ASP. net mvc 2 getting started walkthrough 6-querying data by PAGE

Source: Internet
Author: User

Here we just modified the list.

1. Add the following above list. aspx:Code:

< Form ID = "Frmcreate" Action = "<% = URL. Action (" List "," news ") % > "Method =" Post ">
<% : HTML. dropdownlistfor (M =>   New Cmsnews (). newscategory, New Selectlist ( New Mvc2demo. Models. mvcdemoentities (). cmsnewscategory. tolist (),
" Categorycode " , " Categoryname " ), " = Select = " ) %>
< Input Type = "Submit" Value = "Search"   />

</Form>

2. Modify the paging code in list. aspx.

< Div Class = "Pager" >
<% = Html. Pager (model. pagesize, model. pagenumber, model. totalitemcount, " List " , New {Newscategory = Viewdata [ " Category " ]}) %>
</ Div >

list is the action in newscontroller. New { newscategory = viewdata [ " Category " ]} is the parameter value assignment of newscategory, that is to say, I modified the List action and added the parameter newscategory, therefore, you only need to add more parameters after new And use the "," interval. This should be the object initializer in C #3.0.
3. Modify the newscontroller List action.

///   <Summary>
/// List
/// Writing is too hasty, and the reader can modify it as needed
///   </Summary>
///   <Param name = "page"> Page number </Param>
///   <Param name = "newscategory"> The ID of the dropdownlist is the same as that of the front-end dropdownlist. <%: HTML. dropdownlistfor (M => New cmsnews (). newscategory </Param>
///   <Returns> </returns>
Public Actionresult list ( Int ? Page, string newscategory)
{
// Use viewdata ["category"] to store the value of the selected dropdownlist item

Viewdata [ " Category " ] = Newscategory;
// Number of records displayed per page
Const   Int Defaultpagesize =   1 ;
Int Currentpageindex = Page. hasvalue ? Page. Value -   1 : 0 ;
If ( ! String. isnullorempty (newscategory ))
{
Return View (db. cmsnews. Where (Model => Model. newscategory = Newscategory). orderbydescending (Model => Model. pubdate). tolist (). topagedlist (currentpageindex, defaultpagesize ));
}
Else
{
Return View (db. cmsnews. orderbydescending (Model => Model. pubdate). tolist (). topagedlist (currentpageindex, defaultpagesize ));
}
}

Used hereViewdata ["Category"]To save the value of the selected items in the dropdownlist. Because mvcpaging uses a URL to flip pagesDropdownlistOr there are other methods that haven't been studied yet.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.