csharp, Uncategorized

Sorting in ASP.NET MVC C#

In this article, we will learn how to Sorting in ASP.NET MVC C#.

Step by step learn Sorting in ASP.NET MVC C#

Adding The Model Class
Right-click the Models folder and choose Add -> Class from the menu. Create a new class called Category.

[php]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace ShopStore.Models
{
public partial class Category
{
public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
}
[/php]

Adding a Database Context
Create a new folder in the project named DAL by right-clicking the project ( ShopStore ) in Solution
Explorer. Click Add then New Folder. Now add a new class named StoreContext.cs to the new DAL folder.
write this code.

[php]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using ShopStore.Models;

namespace ShopStore.DAL
{
public class StoreContext:DbContext
{
public DbSet<Category> Categories { get; set; }
}
}
[/php]

sorting-asp-net-mvc-c

Specifying a Connection String
Add a connectionStrings section of the main Web.Config file.
[php]
<connectionStrings>
<add name="StoreContext" connectionString="Data Source=.;AttachDbFilename=|DataDirectory|\ShopStore.mdf;Initial Catalog=ShopStore;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
[/php]

Adding Controllers and Views
1. Right-click the Controllers folder and choose Add -> Controller like screenshot.
sorting-asp-net-mvc-c

2. Open a new window, choose the MVC 5 Controller with views option using Entity Framework.

sorting-asp-net-mvc-c
3. Click Add button and after that, in the Add Controller window, choose this options:
–>Model class: Category
–>Select Data Context class: StoreContext like screenshot

In Categoriescontroller class contains following line of code

[php]
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using ShopStore.DAL;
using ShopStore.Models;

namespace ShopStore.Controllers
{
public class CategoriesController : Controller
{
private StoreContext db = new StoreContext();

// GET: Categories
public ActionResult Index()
{
return View(db.Categories.ToList());
}

// GET: Categories/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Category category = db.Categories.Find(id);
if (category == null)
{
return HttpNotFound();
}
return View(category);
}

// GET: Categories/Create
public ActionResult Create()
{
return View();
}

// POST: Categories/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID,Name,Description")] Category category)
{
if (ModelState.IsValid)
{
db.Categories.Add(category);
db.SaveChanges();
return RedirectToAction("Index");
}

return View(category);
}

// GET: Categories/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Category category = db.Categories.Find(id);
if (category == null)
{
return HttpNotFound();
}
return View(category);
}

// POST: Categories/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "ID,Name,Description")] Category category)
{
if (ModelState.IsValid)
{
db.Entry(category).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(category);
}

// GET: Categories/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Category category = db.Categories.Find(id);
if (category == null)
{
return HttpNotFound();
}
return View(category);
}

// POST: Categories/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Category category = db.Categories.Find(id);
db.Categories.Remove(category);
db.SaveChanges();
return RedirectToAction("Index");
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}
[/php]

You can see here views automatically created Create,Delete,Details,Edit,Index under Views–>Categories folder.

sorting-asp-net-mvc-c

Finally, Write this code for Sorting in ASP.NET MVC C#
Open the Categories Controllers file and updates the Index Action method:
[php]
// GET: Categories
public ActionResult Index()
{
return View(db.Categories.OrderBy(x=>x.Name).ToList());
}
[/php]

Complete code Controllers\CategoriesController.cs Page after apply sorting in Index method.
[php]
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using ShopStore.DAL;
using ShopStore.Models;

namespace ShopStore.Controllers
{
public class CategoriesController : Controller
{
private StoreContext db = new StoreContext();

// GET: Categories
public ActionResult Index()
{
return View(db.Categories.OrderBy(x=>x.Name).ToList());
}

// GET: Categories/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Category category = db.Categories.Find(id);
if (category == null)
{
return HttpNotFound();
}
return View(category);
}

// GET: Categories/Create
public ActionResult Create()
{
return View();
}

// POST: Categories/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID,Name,Description")] Category category)
{
if (ModelState.IsValid)
{
db.Categories.Add(category);
db.SaveChanges();
return RedirectToAction("Index");
}

return View(category);
}

// GET: Categories/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Category category = db.Categories.Find(id);
if (category == null)
{
return HttpNotFound();
}
return View(category);
}

// POST: Categories/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "ID,Name,Description")] Category category)
{
if (ModelState.IsValid)
{
db.Entry(category).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(category);
}

// GET: Categories/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Category category = db.Categories.Find(id);
if (category == null)
{
return HttpNotFound();
}
return View(category);
}

// POST: Categories/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Category category = db.Categories.Find(id);
db.Categories.Remove(category);
db.SaveChanges();
return RedirectToAction("Index");
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}

[/php]

Finally, run your project we can see all the items shown alphabetically.

sorting-asp-net-mvc-c

Leave a Reply

Your email address will not be published.