如何从MVC中的数据库中检索数据并将其显示给webgrid

时间:2016-03-24 08:25:59

标签: c# asp.net-mvc asp.net-mvc-4

现在,在我的应用程序中,程序正在运行,数据库行名称显示在网格中,但数据网格中没有数据,它假设从数据库中显示数据。

MVC控制器:

finish()

但是视觉工作室没有错误显示

这是索引视图:

public ActionResult Index(int? pageNumber, int? pageSize, string filter, string sortColumn, string sortOrder, int? totalCount)

    {
         //List<UserActivityModels> userActivity = null;

        List<UserActivityModels> userActivity = new List<UserActivityModels>();


        //totalCount = 0;

        string sqlWhere = string.Empty;

        sqlWhere = string.IsNullOrEmpty(filter) ? sqlWhere : sqlWhere +
            " AND (Id LIKE @filter)  ";

        string sqlOrderBy = "ORDER BY " + sortColumn + " " + sortOrder;

        String sqlSelect = @"
           SELECT Id
            ,CreatedBy
            ,CreatedOn
            ,ModifiedBy
            ,ModifiedOn
            ,ContactId
            ,EntityName
            ,EntityId
            ,ActivityType
            ,ActivityStatus
            ,DueDate
            ,ActualEndDate
            ,MasqueradeOn
            ,MasqueradeBy 
        FROM UserActivity 
        ORDER BY CreatedOn DESC
       WHERE @Id = Id
       LIMIT @PageSize OFFSET @PageNumber
        ";
        //WHERE @Id = Id
        //LIMIT @PageSize OFFSET @PageNumber
        string sqlCount = @"
        SELECT COUNT(Id) TotalCount 
        FROM UserActivity
              ";


        try
        {
            using (IDbConnection db = new MySqlConnection(ConfigurationManager.ConnectionStrings["CRMPORTALSQLCONN"].ConnectionString))
            {
                userActivity = (List<UserActivityModels>)db.Query<UserActivityModels>(sqlSelect, new

                {
                    @filter = "%" + filter + "%",
                    @PageSize = pageSize,
                    @PageNumber = (pageNumber - 1) * pageSize

                });

                ViewData["totalCount"] = (int)db.ExecuteScalar<int>(sqlCount, new
                {
                    @filter = filter,
                    @PageNumber = pageNumber,
                });
                ViewData["filter"] = filter;
                ViewData["PageSize"] = 30;

                return RedirectToAction("Index");
            }
        }

        catch
        {
            //throw new Exception("Unable to retrieve data from DB.", ex);
            return View(userActivity);
        }

        //return View(userActivity);

        /* if (userActivity != null)
         {
             return RedirectToAction(userActivity);
         }*/
    }


    //POST: /UserActivity/Index
    [HttpPost]
    public ActionResult Index(FormCollection collection)
    {
        try
        {
            // TODO: Add insert logic here

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

是否有人告诉我如何从数据库中检索数据并将其显示到gridview

0 个答案:

没有答案