For last few weeks I have been working on an ASP.Net MVC application that requires use of grid view kind of user interface. Since the user interface was not very complicated I found that use of WebGrid will do the trick. In this post I will discuss simple use of WebGrid extension in MVC framework.
The following code snippet shows simple use of WebGrid extension.
@model IQueryable<WebGridSamples.Models.Post> @{ var grid = new WebGrid(source: Model, defaultSort: "DateCreated", rowsPerPage: 25, canSort:true); } <div> @grid.GetHtml() </div>
As you can see a very simple use of WebGrid requires following.
Displaying data in a grid is not sufficient by itself. If you look at the snapshot of grid in this post, you can see that it is not very intuitive and display itself is not very impressive as well. Therefore it is important that you must be able to control behavior of the grid. As you can see that when I created instance of WebGrid object, I have specified values of some the properties. In this post I have only mentioned four properties, source, defaultSort, rowsPerPage and canSort. The named of these properties itself indicate what this properties are used for. I will discuss these properties in detail in subsequent posts.
So far I have only shown how the code looks like on CSHTML page. The most important part of WebGrid use is data collection that we want to display. Following code snippet shows controller side of the code that sets the collection in Model of this view.
public ActionResult Index() { var blogRepository = new BlogRepository (ConfigurationManager.ConnectionStrings["blogdb"].ConnectionString); var posts= blogRepository.GetAllPosts(); return View(posts); }
There is nothing very complicated about this code. I have used EntityFramework in my prototype application to pull data from my blog database. And then returned it as IQueryable collection in model of view of the page itself. Following code snippet shows the method that I implemented that uses Entity Framework.
public IQueryable<Post> GetAllPosts() { using (var context = new BlogContext(ConnectionString)) { return context.Posts.ToList().AsQueryable(); } }
As you can see how easy it is to WebGrid extension to display grid view in MVC application.
Error loading child ASP.Net application on Windows 2008
How to plan CCSP Exam preparation
Develop a MongoDB pipeline to transform data into time buckets
Alert and Confirm pop up using BootBox in AngularJS
AngularJS Grouped Bar Chart and Line Chart using D3
How to lock and unlock account in Asp.Net Identity provider
2025 © Byteblocks, ALL Rights Reserved. Privacy Policy | Terms of Use