Posted by on Sep 24, 2013 in ASP.NET, Microsoft | 0 comments

In my recent project my client wanted to have index options for authors / manufacturers. Like the following

A B C D.. E

I just wanted to make changes in my cshtml itself and dont want to change anything in the controllers or dont want to make it complicated. I may be very lazy ;-). So I came up with this idea. As you aware, MVC binds Model to view. I am using IList<Authors>. So I used the following code in my view.

<div id=”pagination-bottom” class=”pagination pagination-centered”>
@if (Model.Count > 0)
{
List<string> alpha = Model.Select(u => u.Name.Substring(0, 1)).Distinct().ToList<String>();
alpha.Sort();
<ul class=’unstyled’>
@foreach (string al in alpha)
{
<li><a href=”@Request.Url.Scheme://@Request.Url.Authority@Request.Url.LocalPath?filter=@al”> @al</a></li>
}
</ul>
}
</div>

 

IList<ManufacturerModel> filteredModel;
filteredModel = Model;
var filter = Request.QueryString[“filter”];
if (filter != null)
{
filteredModel = Model.Where(m => m.Name.StartsWith(filter)).ToList<ManufacturerModel>();
}

 

Hope this helps some one in need…