Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to pass data to gridview(Long Post)

How to pass data to gridview(Long Post)

Scheduled Pinned Locked Moved ASP.NET
csharpasp-netdatabaselinqdesign
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    future3839
    wrote on last edited by
    #1

    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>
    
    T 1 Reply Last reply
    0
    • F future3839

      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>
      
      T Offline
      T Offline
      thatraja
      wrote on last edited by
      #2

      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 :-)

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups