本文介绍了在MVC2中绑定GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我是MVC2新手。



我想绑定一个简单的gridview。我花了这个过去一周。

我在谷歌找到了很多代码。但是我很困惑地实现它。



我希望代码绑定mvc2中的gridview而不是mvc3或其他东西。我只想在mvc2中使用它。



我的代码如下。

当我运行它时,它在视图页面给出错误,在foreach循环作为对象引用未设置为对象的实例。



模型:

Hi all, i am new to MVC2.

I am trying to bind a simple gridview. i am tring this for last 1 week.
I found a lot of code in google. but i m very much confused to implement it.

I want code to bind gridview in mvc2 not mvc3 or something else. i want it in mvc2 only.

My code as follow.
When i am running this, its giving error in view page, at foreach loop as Object reference not set to an instance of an object.

MODEL:

public class Users
    {
        string ConnString = ConfigurationManager.AppSettings["ConnString"].ToString();
        DataSet ds;
       
        [Required]
        [Display(Name = "Id")]
        public int ID { get; set; }

        [Required]
        [Display(Name = "Name")]
        public string UserName { get; set; }

        [Required]
        [Display(Name = "Age")]
        public int Age { get; set; }

        [Required]
        [Display(Name = "Gender")]
        public string Gender { get; set; }


        public DataTable GetGridData()
        {
            try
            {
                ds = new DataSet();
                SqlConnection con = new SqlConnection(ConnString);
                SqlDataAdapter ada = new SqlDataAdapter("Select Id,Name,Age,Gender from Emp", ConnString);
                ada.Fill(ds);
                return ds.Tables[0];
            }
            catch (Exception err)
            {
                throw err;
            }
        }
    }





控制器:




CONTROLLER:

public class GridController : Controller
    {
        //
        // GET: /Grid/

        public ActionResult Index()
        {
            DataTable dtGrid = new DataTable();
            Users objGrid = new Users();
            dtGrid = objGrid.GetGridData();

            List<users> Gridd = new List<users>();

            foreach (DataRow dr in dtGrid.Rows)
            {
                Users users = new Users();
                users.ID = Convert.ToInt32(dr["ID"]);
                users.UserName = dr["Name"].ToString();
                users.Age = Convert.ToInt32(dr["Age"]);
                users.Gender = dr["Gender"].ToString();

                Gridd.Add(users);
            }
            return View("Index", Gridd);
        }
    }



AND 查看:


AND VIEW:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MyMVCTest.Models.Users>>" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Index</title>
</head>
<body>
    <div>
        <table width="50%">
            <tr>
                <td width="10%" height="30px">
                    ID
                </td>
                <td width="20%">
                    User Name
                </td>
                <td width="10%">
                    Age
                </td>
                <td width="10%">
                    Gender
                </td>
            </tr>
            <%foreach (var item in Model)
              { %>
            <tr>
                <td>
                    <%=Html.DisplayFor(x=>item.ID) %>
                </td>
                <td>
                    <%=Html.DisplayFor(x => item.UserName)%>
                </td>
                <td>
                    <%=Html.DisplayFor(x => item.Age)%>
                </td>
                <td>
                    <%=Html.DisplayFor(x => item.Gender)%>
                </td>
            </tr>
            <%} %>
        </table>
    </div>
</body>
</html>

推荐答案



这篇关于在MVC2中绑定GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 14:03