title: How to retrieve data from DB in MVC and show it to webgrid
body: <p>Here in my app now, the program is running, the database rows name show in the grid, but there is no data on the datagrid where it suppose to show the data from database.</p>
  
  <p>MVC Controller:</p>
  
  <pre><code>public ActionResult Index(int? pageNumber, int? pageSize, string filter, string sortColumn, string sortOrder, int? totalCount)
  
      {
           //List&lt;UserActivityModels&gt; userActivity = null;
  
          List&lt;UserActivityModels&gt; userActivity = new List&lt;UserActivityModels&gt;();
  
  
          //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&lt;UserActivityModels&gt;)db.Query&lt;UserActivityModels&gt;(sqlSelect, new
  
                  {
                      @filter = "%" + filter + "%",
                      @PageSize = pageSize,
                      @PageNumber = (pageNumber - 1) * pageSize
  
                  });
  
                  ViewData["totalCount"] = (int)db.ExecuteScalar&lt;int&gt;(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();
          }
      }
  </code></pre>
  
  <p>but there is no error show on the visual studio</p>
  
  <p>and here is index view:</p>
  
  <pre><code>    @*&lt;p&gt;
      @Html.ActionLink("Create New", "Create")
  &lt;/p&gt;*@
  
     &lt;h3&gt;User Activity&lt;/h3&gt;
     @using (Html.BeginForm("index", null, FormMethod.Get))
     {
  &lt;div class="row"&gt;
      &lt;div class="col-sm-8"&gt;
          &lt;div class="input-group"&gt;
  
              &lt;input type="text"
                     name="filter"
                     value="@ViewBag.filter"
                     class="form-control"
                     style="display: inline"
                     placeholder="Search by Contact Id" /&gt;
              &lt;span class="input-group-btn"&gt;
                  &lt;button class="btn btn-default" type="submit"&gt;Go&lt;/button&gt;
              &lt;/span&gt;
  
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="pull-right col-lg-1"&gt;
          &lt;a class="btn btn-success" data-modal="" href="/UserActivity/Create" id="btnCreate"&gt;
              &lt;span class="glyphicon glyphicon-plus"&gt;&lt;/span&gt;
          &lt;/a&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div class="table-responsive" style="margin-top:5px;"&gt;
      @{
          var grid = new WebGrid(
                      Model,
                      canPage: true,
                      rowsPerPage: 5,
                      canSort: true);
          //ajaxUpdateContainerId: "grid");
  
          //grid.Bind(Model, rowCount: (int)ViewData["totalCount"], autoSortAndPage: false);
  
          grid.Pager(WebGridPagerModes.All);
  
          @grid.GetHtml(
         //htmlAttributes: new { id = "grid" },
      // id for ajaxUpdateContainerId parameter
      fillEmptyRows: false,
      tableStyle: "table table-striped",
      mode: WebGridPagerModes.All,
      columns: grid.Columns(
        //grid.Column("Id", "Id"),
        grid.Column("Id", "Id", style: "col-lg-1", canSort: true),
        grid.Column("CreatedBy", "CreatedBy", style: "col-lg-6"),
        grid.Column("CreatedOn", header: "CreatedOn", style: "col-lg-2", canSort: true),
        grid.Column("ModifiedBy", header: "ModifiedBy", style: "col-lg-2"),
        grid.Column("ModifiedOn", header: "ModifiedOn", style: "col-lg-2"),
        grid.Column("ContactId", header: "ContactId", style: "col-lg-2"),
        grid.Column("EntityName", header: "EntityName", style: "col-lg-2"),
        grid.Column("EntityId", header: "EntityId", style: "col-lg-2"),
        grid.Column("ActivityType", header: "ActivityType", style: "col-lg-2"),
        grid.Column("ActivityStatus", header: "ActivityStatus", style: "col-lg-2"),
        grid.Column("DueDate", header: "DueDate", style: "col-lg-2"),
        grid.Column("ActualEndDate", header: "ActualEndDate", style: "col-lg-2"),
        grid.Column("MasqueradeOn", header: "MasqueradeOn", style: "col-lg-2"),
        grid.Column("MasqueradeBy", header: "MasqueradeBy", style: "col-lg-2"),
  
  
        grid.Column(header: "Action", canSort: false, style: "action",
   format: @&lt;text&gt;
          @Html.Raw("&lt;a data-modal='' href='/UserActivity/Details/" + item.Id + "' id='" + item.Id + "' title='Detail'&gt; &lt;span class='glyphicon glyphicon-search'&gt; &lt;/span&gt; &lt;/a&gt;")
          @Html.Raw("&lt;a data-modal='' href='/UserActivity/edit/" + item.Id + "' id='" + item.Id + "' title='Edit'&gt; &lt;span class='glyphicon glyphicon-edit'&gt; &lt;/span&gt; &lt;/a&gt;")
          @Html.Raw("&lt;a data-modal='' href='/UserActivity/delete/" + item.Id + "' id='" + item.Id + "' title='Delete'&gt; &lt;span class='glyphicon glyphicon-trash'&gt; &lt;/span&gt; &lt;/a&gt;")
  &lt;/text&gt;)
  
      ));
      }
  
  &lt;/div&gt;
          }
  </code></pre>
  
  <p></p>
  
  <p>is anyone coud tell me how to retrieve data from db and show it into gridview</p>