Friday, December 5, 2014

alphabetical pager in mvc



Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NewDesiging.Models
{
    public class AlphabeticalPagingViewModel
    {
        public List<string> ProductNames { get; set; }
        public List<string> Alphabet
        {
            get
            {
                var alphabet = Enumerable.Range(65, 26).Select(i => ((char)i).ToString()).ToList();
                alphabet.Insert(0, "All");
                alphabet.Insert(1, "0-9");
                return alphabet;
            }
        }
        public List<string> FirstLetters { get; set; }
        public string SelectedLetter { get; set; }
        public bool NamesStartWithNumbers
        {
            get
            {
                var numbers = Enumerable.Range(0, 10).Select(i => i.ToString());
                return FirstLetters.Intersect(numbers).Any();
            }
        }
    }
    //public class AlphabeticalPagingViewModel
    //{
    //    public List<string> ProductNames { get; set; }
    //    public List<string> FirstLetters { get; set; }
    //    public string SelectedLetter { get; set; }
    //}
}

Html helpers
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace NewDesiging.Models
{
    public static class HtmlHelpers
    {
        public static HtmlString AlphabeticalPager(this HtmlHelper html, string selectedLetter, IEnumerable<string> firstLetters, Func<string, string> pageLink)
        {
            var sb = new StringBuilder();
            var numbers = Enumerable.Range(0, 10).Select(i => i.ToString());
            var alphabet = Enumerable.Range(65, 26).Select(i => ((char)i).ToString()).ToList();
            alphabet.Insert(0, "All");
            alphabet.Insert(1, "0-9");

            var ul = new TagBuilder("ul");
            ul.AddCssClass("pagination");
            ul.AddCssClass("alpha");

            foreach (var letter in alphabet)
            {
                var li = new TagBuilder("li");
                if ( letter == "All")
                {
                    if (selectedLetter == letter || selectedLetter== null && letter == "All")
                    {
                        li.AddCssClass("active");
                        var span = new TagBuilder("span");
                        span.SetInnerText(letter);
                        li.InnerHtml = span.ToString();
                    }
                    else
                    {
                        var a = new TagBuilder("a");
                        a.MergeAttribute("href", pageLink(letter));
                        a.InnerHtml = letter;
                        li.InnerHtml = a.ToString();
                        a.AddCssClass("active");
                        //li.AddCssClass("active");
                    }
                }
                 else if(selectedLetter==letter)
                {
                    li.AddCssClass("active");
                    var span = new TagBuilder("span");
                    span.SetInnerText(letter);
                    li.InnerHtml = span.ToString();
                    //var span = new TagBuilder("span");
                    //span.SetInnerText(letter);
                    //li.InnerHtml = span.ToString();
                }
                else
                {
                    var a = new TagBuilder("a");
                    //li.AddCssClass("inactive");
                    a.AddCssClass("active");
                    a.MergeAttribute("href", pageLink(letter));
                    a.InnerHtml = letter;
                  
                    li.InnerHtml = a.ToString();
                    //var span = new TagBuilder("span");
                    //span.SetInnerText(letter);
                    //li.InnerHtml = span.ToString();
                }
                sb.Append(li.ToString());
            }
            ul.InnerHtml = sb.ToString();
            return new HtmlString(ul.ToString());
        }
    }
}
Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NewDesiging.Models;

namespace NewDesiging.Controllers
{
    public class SearchController : Controller
    {
        // GET: Search
        //public ActionResult Index()
        //{
        //    return View();
        //}
        public ActionResult Index(string selectedLetter)
        {
            var model = new AlphabeticalPagingViewModel { SelectedLetter = selectedLetter };
            using (var context = new NareshDb())
            {
                model.FirstLetters = context.Jobs
                    .GroupBy(p => p.Title.Substring(0, 1))
                    .Select(x => x.Key.ToUpper()).Take(100)
                    .ToList();

                if (string.IsNullOrEmpty(selectedLetter) || selectedLetter == "All")
                {
                    model.ProductNames = context.Jobs
                        .Select(p => p.Title).Take(100)
                        .ToList();
                }
                else
                {
                    if (selectedLetter == "0-9")
                    {
                        var numbers = Enumerable.Range(0, 10).Select(i => i.ToString());
                        model.ProductNames = context.Jobs
                            .Where(p => numbers.Contains(p.Title.Substring(0, 1)))
                            .Select(p => p.Title).Take(100)
                            .ToList();
                    }
                    else
                    {
                        model.ProductNames = context.Jobs
                            .Where(p => p.Title.StartsWith(selectedLetter))
                            .Select(p => p.Title).Take(100)
                            .ToList();
                    }
                }
            }
            return View(model);
        }
    }
}
View
@using NewDesiging.Models
@model NewDesiging.Models.AlphabeticalPagingViewModel

@*<ul class="pagination alpha">
    @foreach (var link in Model.Alphabet)
    {
        if (Model.FirstLetters.Contains(link) || (Model.NamesStartWithNumbers && link == "0-9") || link == "All")
        {
            if (link == "All")
            {
                <li class="active"><span>@link</span></li>
            }
            else
            {
                <li>@Html.ActionLink(link, "Index", new { selectedLetter = link })</li>
            }
        }
        else
        {
            <li class="inactive"><span>@link</span></li>
        }
    }
</ul>
<ul>
  

    @foreach (var item in Model.ProductNames)
    {
      
            <li>@item</li>
           
        }
    }


    @foreach (var item in Model.ProductNames)
        {
            <li>@item</li>
        }
    </ul>*@
@Html.AlphabeticalPager(Model.SelectedLetter, Model.FirstLetters, x =>Url.Action("Index", new { selectedLetter = x }))
<ul>
  
   
    @foreach (var item in Model.ProductNames)
    {
       
                                 <li>@item</li>
                                   
                                    
    }
  
</ul>


Sunday, November 23, 2014

Difference between Asp.Net MVC and Web Forms



Difference between Asp.Net MVC and Web Forms
Asp.Net Web Forms
Asp.Net MVC
Asp.Net Web Form follow a traditional event driven development model.
Asp.Net MVC is a lightweight and follow MVC (Model, View, Controller) pattern based development model.
Asp.Net Web Form has server controls.
Asp.Net MVC has html helpers.
Asp.Net Web Form supports view state for state management at client side.
Asp.Net MVC does not support view state.
Asp.Net Web Form has file-based URLs means file name exist in the URLs must have its physically existence.
Asp.Net MVC has route-based URLs means URLs are divided into controllers and actions and moreover it is based on controller not on physical file.
Asp.Net Web Form follows Web Forms Syntax
Asp.Net MVC follow customizable syntax (Razor as default)
In Asp.Net Web Form, Web Forms(ASPX) i.e. views are tightly coupled to Code behind(ASPX.CS) i.e. logic.
In Asp.Net MVC, Views and logic are kept separately.
Asp.Net Web Form has Master Pages for consistent look and feels.
Asp.Net MVC has Layouts for consistent look and feels.
Asp.Net Web Form has User Controls for code re-usability.
Asp.Net MVC has Partial Views for code re-usability.
Asp.Net Web Form has built-in data controls and best for rapid development with powerful data access.
Asp.Net MVC is lightweight, provide full control over markup and support many features that allow fast & agile development. Hence it is best for developing interactive web application with latest web standards.
Asp.Net Web Form is not Open Source.
Asp.Net Web MVC is an Open Source.