But in controller.cs i am not getting to the control GridView1.
ASP.NET MVC does not work with server controls. You can't just drag a grid view on the page and access it from the controller. That's not how the asp.net mvc framework behaves. You cannot do that.
In MVC the grid is the reposibility of the view and not the controller. The controller only provides data to be consumed by the view.
There are also more advanced grids provided by third party companies. Take a look at the Telerik Grid which is open source (free to use if your company is ok with open source) http://demos.telerik.com/aspnet-mvc/grid
I'm trying to do the same thing and I was looking at that WebGrid article in MSDN Magazine. It looks like the answer I need, but being new to MVC I don't understand exactly how to hook it up. In the article it looks like the controller gets a IEnumerable<Product>
object from somewhere. I don't understand the IEnumerable<> syntax but I think it's saying any class that inherits from IEnumerable. So is Product a Microsoft class or it's just a class built for that example? Then is it passed back to the View by using
the model keyword? I tried that in my example but I get an error that the namespace Helpers doesn't exist in System.Web. I can see that I added a reference to System.Web.Helpers in my project. I then added the line below in the View class. If you would
be so kind, can you tell me what I am doing wrong?
Hi steve - An IEnumerable is a basically a collection (similar to an array, but with more functionality such as deletion, addition and more). IEnumerable < > is a generic collection that lets you basically make it a strongly typed collection that is bound
to a particular class. For example IEnumerable<Product> means this is a collection of products. Product isn't a microsoft class, but just used as an example.
With MVC you want to create model classes that map directly to business objects or in some cases database tables. You pass the model in the controller with something like this:
public ActionResult Index(){
// Get all products from the database and assign to the variable products
IEnumerable<Products> products = db.Products.ToList();
//return a view with products as the input parameter. This assigns products to the view's Model object
return View(products);
}
You shouldn't need to Import System.Web.Helpers. Are you working with mvc3? if so, you should also consider using the Razor view engine. Check out this great tutorial that introduces all these concepts in mvc and walks you through building a new applicationg
from scratch. http://www.asp.net/mvc/tutorials/mvc-music-store
How does the View class receive the Products data from the Controller? Does it use a keyword like Model that I saw in the MSDN Magazine example?
I'm still stuck on the Helpers error. I don't know if I'm using MVC 3. I'm using Visual Web Developer Express 2010.
Compiler Error Message: CS0234: The type or namespace name 'Helpers' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)
Source Error:
Line 1: <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
Line 2: <%@ Import Namespace="System.Web.Helpers" %>
Line 3:
Line 4: <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
What type of project did you create? Try removing the Namespace import. Also, how are you referencing the Html helper? (ie <%=Html.ActionLink("Home","Index","Home")%> or something like that?
[I'm reposting this because the forum software here is a little buggy on edits]
How does the Viewclass receive the Products data
from the Controller?Does it use a keyword like
Model that I saw in the MSDN
Magazine?
I'm still stuck on the Helpers error. I don't know
if I'm using MVC 3. I'm
usingVisualWebDeveloperExpress2010.
Compiler Error Message: CS0234: The type or namespace name 'Helpers' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)
Source Error:
Line 1: <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
Line 2: <%@ Import Namespace="System.Web.Helpers" %>
Line 3:
Line 4: <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
The view receives the product data from the action method. In the example above I passed the products collection to the view via the "View()" method. In the view whatever gets passed in becomes the Model and is accessible from the property "Model".
Try creating a new project and starting from scratch, make sure you pick new MVC3 project with Razor. That should be an available option under VWD (visual web developer) 2010.
arunrv1985
Member
51 Points
39 Posts
Diaplaying database values on gridview in MVC
Apr 04, 2012 11:00 AM|LINK
Hello every one
I want to display data on GridView1 for that i have taken a Gridview in my View page . And its control event is all written the controller.cs
But in controller.cs i am not getting to the control GridView1.
so can any one tell or suggest any link/code to solve it .
Advance thanks
Arun
Gaspard
Contributor
2066 Points
416 Posts
Re: Diaplaying database values on gridview in MVC
Apr 04, 2012 11:22 AM|LINK
I am not expert in asp.net MVC but I think you can not have GridView (Web Form) in Asp.net MVC
There are some alternative solutions, have look at this web site
http://www.schnieds.com/2010/01/gridview-in-aspnet-mvc.html
Maybe I am wrong, I am still learning Asp.net MVC!
Regards
ignatandrei
All-Star
134491 Points
21566 Posts
Moderator
MVP
Re: Diaplaying database values on gridview in MVC
Apr 04, 2012 11:36 AM|LINK
And you will never get. PLease see http://www.asp.net/mvc why you do NOT need and do NOT must have this one.
CodeHobo
All-Star
18647 Points
2647 Posts
Re: Diaplaying database values on gridview in MVC
Apr 04, 2012 03:05 PM|LINK
ASP.NET MVC does not work with server controls. You can't just drag a grid view on the page and access it from the controller. That's not how the asp.net mvc framework behaves. You cannot do that.
In MVC the grid is the reposibility of the view and not the controller. The controller only provides data to be consumed by the view.
Instead of the default GridView, MVC comes with a WebGrid.
http://msdn.microsoft.com/en-us/magazine/hh288075.aspx
There are also more advanced grids provided by third party companies. Take a look at the Telerik Grid which is open source (free to use if your company is ok with open source)
http://demos.telerik.com/aspnet-mvc/grid
But you can use any client side grid as well such as KendoUI
http://demos.kendoui.com/web/grid/index.html
or jqGrid
http://www.trirand.com/blog/
Blog | Twitter : @Hattan
DallasSteve
Member
221 Points
331 Posts
Re: Diaplaying database values on gridview in MVC
Apr 16, 2012 09:35 PM|LINK
CodeHobo
I'm trying to do the same thing and I was looking at that WebGrid article in MSDN Magazine. It looks like the answer I need, but being new to MVC I don't understand exactly how to hook it up. In the article it looks like the controller gets a IEnumerable<Product> object from somewhere. I don't understand the IEnumerable<> syntax but I think it's saying any class that inherits from IEnumerable. So is Product a Microsoft class or it's just a class built for that example? Then is it passed back to the View by using the model keyword? I tried that in my example but I get an error that the namespace Helpers doesn't exist in System.Web. I can see that I added a reference to System.Web.Helpers in my project. I then added the line below in the View class. If you would be so kind, can you tell me what I am doing wrong?
<%@ Import Namespace="System.Web.Helpers" %>
www.SteveGaines.us
CodeHobo
All-Star
18647 Points
2647 Posts
Re: Diaplaying database values on gridview in MVC
Apr 16, 2012 09:42 PM|LINK
Hi steve - An IEnumerable is a basically a collection (similar to an array, but with more functionality such as deletion, addition and more). IEnumerable < > is a generic collection that lets you basically make it a strongly typed collection that is bound to a particular class. For example IEnumerable<Product> means this is a collection of products. Product isn't a microsoft class, but just used as an example.
With MVC you want to create model classes that map directly to business objects or in some cases database tables. You pass the model in the controller with something like this:
public ActionResult Index(){ // Get all products from the database and assign to the variable products IEnumerable<Products> products = db.Products.ToList(); //return a view with products as the input parameter. This assigns products to the view's Model object return View(products); }You shouldn't need to Import System.Web.Helpers. Are you working with mvc3? if so, you should also consider using the Razor view engine. Check out this great tutorial that introduces all these concepts in mvc and walks you through building a new applicationg from scratch. http://www.asp.net/mvc/tutorials/mvc-music-store
Blog | Twitter : @Hattan
DallasSteve
Member
221 Points
331 Posts
Re: Diaplaying database values on gridview in MVC
Apr 16, 2012 09:48 PM|LINK
www.SteveGaines.us
CodeHobo
All-Star
18647 Points
2647 Posts
Re: Diaplaying database values on gridview in MVC
Apr 16, 2012 09:50 PM|LINK
What type of project did you create? Try removing the Namespace import. Also, how are you referencing the Html helper? (ie <%=Html.ActionLink("Home","Index","Home")%> or something like that?
Blog | Twitter : @Hattan
DallasSteve
Member
221 Points
331 Posts
Re: Diaplaying database values on gridview in MVC
Apr 16, 2012 09:51 PM|LINK
CodeHobo
[I'm reposting this because the forum software here is a little buggy on edits]
How does the View class receive the Products data from the Controller? Does it use a keyword like Model that I saw in the MSDN Magazine?
I'm still stuck on the Helpers error. I don't know if I'm using MVC 3. I'm using Visual Web Developer Express 2010.
www.SteveGaines.us
CodeHobo
All-Star
18647 Points
2647 Posts
Re: Diaplaying database values on gridview in MVC
Apr 16, 2012 09:53 PM|LINK
The view receives the product data from the action method. In the example above I passed the products collection to the view via the "View()" method. In the view whatever gets passed in becomes the Model and is accessible from the property "Model".
Try creating a new project and starting from scratch, make sure you pick new MVC3 project with Razor. That should be an available option under VWD (visual web developer) 2010.
Blog | Twitter : @Hattan