How to pass data to gridview(Long Post)
-
Hi, every one. I am new in MVC and just practicing to improve my knowledge.I have the following codes and trying to pass data to GridView. but I get error after running the page. Its my PersonController
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using MvcApplication1.ViewModels;
using MvcApplication1.Models;namespace MvcApplication1.Controllers
{
public class PersonController : Controller
{
HostelEntities1 HostelDB = new HostelEntities1();
//
// GET: /Person/public ActionResult Index() { var Persons = from peson in HostelDB.Person\_prs select peson.fname\_prs; var ViewModel = new PersonIndexViewModel { Person = Persons.ToList(), NumberOfPeople= Persons.Count() }; return View(ViewModel); } }
}
Its my ViewModel Class. I have class called PersonIndexViewModel
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;namespace MvcApplication1.ViewModels
{
public class PersonIndexViewModel
{
public int NumberOfPeople { set; get; }
public List<string> Person { set; get; }
}
}and finally in View folder I have the following code
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication1.ViewModels.PersonIndexViewModel>" %>
<%@ Register assembly="System.Web.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" namespace="System.Web.UI.WebControls" tagprefix="asp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Index
</asp:Content>
<script type="text/C#" runat=server>
protected void Page_Load(object sender, EventArgs e)
{
this.GridView1.DataSource = this.Model.Person;
this.GridView1.DataBind();
}</script>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>This Page List Name Of People</h2> <p>select from<%=Model.NumberOfPeople%></p> <ul> <%foreach (string NamePeople in Model.Person) { %> <li> <%=NamePeople%> </li>
-
Hi, every one. I am new in MVC and just practicing to improve my knowledge.I have the following codes and trying to pass data to GridView. but I get error after running the page. Its my PersonController
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using MvcApplication1.ViewModels;
using MvcApplication1.Models;namespace MvcApplication1.Controllers
{
public class PersonController : Controller
{
HostelEntities1 HostelDB = new HostelEntities1();
//
// GET: /Person/public ActionResult Index() { var Persons = from peson in HostelDB.Person\_prs select peson.fname\_prs; var ViewModel = new PersonIndexViewModel { Person = Persons.ToList(), NumberOfPeople= Persons.Count() }; return View(ViewModel); } }
}
Its my ViewModel Class. I have class called PersonIndexViewModel
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;namespace MvcApplication1.ViewModels
{
public class PersonIndexViewModel
{
public int NumberOfPeople { set; get; }
public List<string> Person { set; get; }
}
}and finally in View folder I have the following code
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication1.ViewModels.PersonIndexViewModel>" %>
<%@ Register assembly="System.Web.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" namespace="System.Web.UI.WebControls" tagprefix="asp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Index
</asp:Content>
<script type="text/C#" runat=server>
protected void Page_Load(object sender, EventArgs e)
{
this.GridView1.DataSource = this.Model.Person;
this.GridView1.DataBind();
}</script>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>This Page List Name Of People</h2> <p>select from<%=Model.NumberOfPeople%></p> <ul> <%foreach (string NamePeople in Model.Person) { %> <li> <%=NamePeople%> </li>
Try this CodeSnip: Exporting GridView to Excel[^]
thatraja |Chennai|India|
Brainbench certifications
Down-votes are like kid's kisses don't reject it :-)
Do what you want quickly because the Doomsday on 2012 :-)