MVC3 Dynamic Search Paging using PagedList
Tech-Today

MVC3 Dynamic Search Paging using PagedList


Basically there's a great tutorial from this site:
Unboxed solutions.
-I've copied the codes and add some comments.

I'll just try to add my own comments.

1.) First you need to install PagedList.Mvc from Nuget.
a.) Go to Tools->Library Package Manager->Package Manager Console
b.) In the Package Manager Console Tab execute:
Install-Package PagedList.Mvc
-It will add a reference to your project and add PagedList.css.

What you should noticed:
1.) All the fields have getter and setter.
//ViewModel
using PagedList;
namespace SearchFormResultPagingExample.Models {
public class SearchViewModel {
public int? Page { get; set; }
public string EmailAddress { get; set; }
public string LastName { get; set; }
public IPagedList SearchResults { get; set; }
public string SearchButton { get; set; }
}
}

2.) No need to add Take(start, count) to the query.
//Controller
using System.Linq;
using System.Web.Mvc;
using SearchFormResultPagingExample.Models;
using PagedList; //NOTE: use Nuget to reference PagedList

namespace SearchFormResultPagingExample.Controllers {
public class SearchController : Controller {
const int RecordsPerPage = 25;

public ActionResult Index(SearchViewModel model) {
if (!string.IsNullOrEmpty(model.SearchButton) || model.Page.HasValue) {
var entities = new AdventureWorksEntities();
var results = entities.Contacts.Where(c => c.LastName.StartsWith(model.LastName) && c.EmailAddress.StartsWith(model.EmailAddress))
.OrderBy(o => o.LastName);
var pageIndex = model.Page ?? 0;
model.SearchResults = results.ToPagedList(pageIndex, 25);
}
return View(model);
}
}
}

3.) Form action should be Get
//View
@model SearchFormResultPagingExample.Models.SearchViewModel
@using PagedList.Mvc;

@using (Html.BeginForm("Index", "Search", FormMethod.Get)) {
@Html.ValidationSummary(false)
<fieldset>
<legend>Contact Searchlegend>
<div class="editor-label">
@Html.LabelFor(model => model.EmailAddress)
div>
<div class="editor-field">
@Html.EditorFor(model => model.EmailAddress)
@Html.ValidationMessageFor(model => model.EmailAddress)
div>
<div class="editor-label">
@Html.LabelFor(model => model.LastName)
div>
<div class="editor-field">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
div>
<p>
<input name="SearchButton" type="submit" value="Search" />
p>
fieldset>
}

@if (Model.SearchResults != null && Model.SearchResults.Count > 0) {
foreach (var result in Model.SearchResults) {
<hr />
<table >
<tr>
<td valign="top" >
<div >@result.LastName, @result.FirstNamediv>
@result.Title<br />
@result.Phone<br />
@result.EmailAddress
<td>
<tr>
<table>
}
<hr />
@Html.PagedListPager(Model.SearchResults,
page => Url.Action("Index", new RouteValueDictionary() {
{ "Page", page },
{ "EmailAddress", Model.EmailAddress },
{ "LastName", Model.LastName }
}),
PagedListRenderOptions.PageNumbersOnly)
}




- How To Create An Xsd And Xml Data From Model Entity In Java
The following code will show how to generate a xsd base on a given model and a sample xml with data. Let's assume you have a CustomerList model, which is an array of Customer. JAXBContext context; try { context = JAXBContext.newInstance(CustomerList.class);...

- Mvc3 Ajax Validation Doesn't Work On A Jquery Served Form
To make automatic validation using data annotation on a model on mvc3 c# we need: 1.) A model with data annotation (ex. required attribute). 2.) Form with jquery declaration (jquery1.7, validate, validate unobtrusive, and unobtrusive-ajax. All are available...

- How To Upload A File In An Mvc3 C# Ajax Form
It's easy to upload file in a synchronous form, where you just post the file and read a HttpPostedFile variable in the server side: In plain html form, take note of the form enctype property: //the view (index.cshtml) @using (Html.BeginForm("Upload",...

- How To Read And Write An Object In A Textfile In C#
Long time ago I've a requirement to read sms messages from device to be written in a text file. Before I learned to serialize an object, I was saving it in a file comma-delimited. That approach is working but sometimes it's a pain to keep track...

- How To Use Entityframework Codefirst To Update Your Database
This tutorial will attempt to explain how to update an already existing database using EntityFramework's CodeFirst approach. We need: 1.) VS2010 SP1 2.) NuGet 3.) EntityFramework 4.1 4.) EntityFramework.Migrations   Steps: 1.) We need to make...



Tech-Today








.