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.