Multiple types of ASP.NET MVC Views Implementation


In ASP.net MVC we have three parts Model, View and Container. The View part can be written in several ways. Razor view is one of the most widely used format.We can also have it implemented using Jquery. Lets have a spin around how to write views Deffrently

Lets Start and learn how to define Model Classes for a application and how to map them with POCO(Plain Old CLR Objects) Entities

Writing the Model part



In order to create the Model, add the following class under the "Model" folder:

Book.cs

public class Book
{
public string BookID { get; set; }
public string BookName { get; set; }
public int Quantity { get; set; }
public decimal Price { get; set; }
}



Every Model Needs a Controller to decide what to do and how ?

Writing the Controller Part





public class BookController : Controller
{
//
// GET: /Book/

public ActionResult Index()
{
return View();
}
}



Add Sample Data





public ActionResult BookList()
{
List BookList= new List{
new Book{BookID="P01",BookName="DaVinciCode",Quantity=10,Price=12},
new Book{BookID="P02",BookName="Three States",Quantity=12,Price=20},
new Book{BookID="P03",BookName="Dracula",Quantity=15,Price=22},
new Book{BookID="P04",BookName="Premchand",Quantity=20,Price=27}
};
return View();
}



Writing Action methods, action results, and routing



In global.asax.cs defines the default routing pattern, {controller}/{action}/{id}, as follows:

routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters



1)First type - Writing a View using the ASPX engine



In the page load write the following code.


public partial class _Default : Page
{
public void Page_Load(object sender, System.EventArgs e)
{
HttpContext.Current.RewritePath(Request.ApplicationPath);
IHttpHandler httpHandler = new MvcHttpHandler();
httpHandler.ProcessRequest(HttpContext.Current);
}
}




2)Second type - Writing a View using the Razor engine



Let's create one more model Library:

Library.cs



public class Library
{
public string LibraryID { get; set; }
public string LibraryName { get; set; }
public string LibraryAddress { get; set; }
}



Sample Data






public ActionResult LibraryList()
{
List LibraryList = new List{
new Library{LibraryID="S01", LibraryName="Kausik",LibraryAddress="Kolkata"},
new Library{LibraryID="S02", LibraryName="Sandeep",LibraryAddress="Delhi"},
new Library{LibraryID="S03", LibraryName="Soumick",LibraryAddress="Chennai"},
new Library{LibraryID="S04", LibraryName="Arindam",LibraryAddress="Mumbai"}
};
return View();
}



3)Third Type of writing a Views using jQuery



Transaction.cs



public class Transaction
{
public string TransactionID { get; set; }
public int TransactionQty { get; set; }
public decimal TransactionPrice { get; set; }
}




public JsonResult GetTransactionList()
{
List TransactionList = new List{
new Transaction{ TransactionID="101", TransactionQty=10, TransactionPrice=12},
new Transaction{ TransactionID="201", TransactionQty=12, TransactionPrice=15},
new Transaction{ TransactionID="301", TransactionQty=15, TransactionPrice=17}
};
return Json(TransactionList, JsonRequestBehavior.AllowGet);
}




Add this code in controller method





public ActionResult TransactionList()
{
return View("TransactionView");
}





Add Jquery Reference





You are all set to go !

Happy Coding :)


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: