.Net Interview Questions and Answers

Search:

ASP.NET MVC interview questions: - What is WebGrid in MVC and how to use it?

By : Shiv Prasad Koirala | Apr 14th, 2014 | Views : 3631 | Comments : 0 Bookmark and Share

WebGrid is a helper introduced in MVC 4 which will let us render tabular data in a very simple manner. It has inbuilt support for sorting, paging, asynchronous updates via ajax and custom column formatting.


Following are the steps to implement WebGrid.


Step 1 - Create Model Data


First we need data which we will use to bind webgrid. We may get this as a model or may be as ViewData. In our case we assume that we will get as Model.




Step 2 - Make your view a strongly typed view


@model List


Step 3 - Create instance of WebGrid


WebGridMyGrid = new WebGrid(Model);


Write following code in your view,


Step 4-Render Grid


Write following code in your view,


@grid.GetHtml()


Output


CustomerId

CustomerName

CustomerAddress

1

A

AA

2

B

BB

3

C

CC


Customizing WebGrid


  • Paging - For that we will use overloaded version of webgrid constructor as follows,


WebGridMyGrid = new WebGrid(Model, rowsPerPage:3);


Output (considering we have total 10 records in the model)



CustomerId

CustomerName

CustomerAddress

1

A

AA

2

B

BB

3

C

CC

1 2 3 4 >


  • Customize columns - We will use overloaded version of GetHtml Method for that as follows,


@grid.GetHtml

(

            columns:grid.Columns

            (

                        grid.Column("CustomerId", "Customer Id"),

                        grid.Column("CustomerName", "Customer Name"),

                        grid.Column("CustomerAddress", "CustomerAddress")

            )

)


Output


CustomerId

CustomerName

CustomerAddress

1

A

AA

2

B

BB

3

C

CC


Hope you enjoyed reading this and learned something new. Your comments and votes are always welcome.


If you are looking for some hard core live training on MVC, WCF, Design Patterns, BI, WPF, TFS, HTML5 visit www.sukesh-Marla.com or contact SukeshMarla@Gmail.com.


Also see following ASP.NET MVC interview questions video on MVC Asynch controllers and thread starvation: -



You can also read this article which talks in details about MVC WebGrid from here

http://www.codeproject.com/Articles/843788/WebGrid-in-ASP-NET-MVC-important-tips

 
Rate this Article :
1 Star
2 Stars
3 Stars
4 Stars
5 Stars
1 1 1 2 1
 
 
 
 

.NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala

In this article we will explain .NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala.... Read More

What is the difference between Stored Procedures and Function (SQL Server interview question with answers)?

In this article we will explain what is the difference between Stored Procedures and Function.For more articles and videos visit us on www.questpond.com... Read More

Explain MVC application life cycle?

In this article we will explain MVC application life cycle.For more articles and videos visit us on www.questpond.com... Read More

C# Design pattern interview questions with answers: - How Singleton is different from Static class?

In this article we will explain How Singleton is different from Static class.... Read More

WCF interview questions and answers: - What is the difference between Service endpoint and Client endpoint?

In this article we will explain What is the difference between Service endpoint and Client endpoint.... Read More

.NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala

In this article we will explain .NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala.... Read More

What is the difference between Stored Procedures and Function (SQL Server interview question with answers)?

In this article we will explain what is the difference between Stored Procedures and Function.For more articles and videos visit us on www.questpond.com... Read More

Explain MVC application life cycle?

In this article we will explain MVC application life cycle.For more articles and videos visit us on www.questpond.com... Read More

C# Design pattern interview questions with answers: - How Singleton is different from Static class?

In this article we will explain How Singleton is different from Static class.... Read More

WCF interview questions and answers: - What is the difference between Service endpoint and Client endpoint?

In this article we will explain What is the difference between Service endpoint and Client endpoint.... Read More

Article Categories

Learn in Hindi