C# 如何在MVC中从数据库中检索数据并将其显示给webgrid
现在,在我的应用程序中,程序正在运行,数据库行名称显示在网格中,但datagrid上没有数据,它假定在其中显示来自数据库的数据 MVC控制器:C# 如何在MVC中从数据库中检索数据并将其显示给webgrid,c#,asp.net-mvc,asp.net-mvc-4,C#,Asp.net Mvc,Asp.net Mvc 4,现在,在我的应用程序中,程序正在运行,数据库行名称显示在网格中,但datagrid上没有数据,它假定在其中显示来自数据库的数据 MVC控制器: public ActionResult Index(int? pageNumber, int? pageSize, string filter, string sortColumn, string sortOrder, int? totalCount) { //List<UserActivityModels> us
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();
}
}
但是在VisualStudio上没有错误显示
这是索引视图:
@*<p>
@Html.ActionLink("Create New", "Create")
</p>*@
<h3>User Activity</h3>
@using (Html.BeginForm("index", null, FormMethod.Get))
{
<div class="row">
<div class="col-sm-8">
<div class="input-group">
<input type="text"
name="filter"
value="@ViewBag.filter"
class="form-control"
style="display: inline"
placeholder="Search by Contact Id" />
<span class="input-group-btn">
<button class="btn btn-default" type="submit">Go</button>
</span>
</div>
</div>
<div class="pull-right col-lg-1">
<a class="btn btn-success" data-modal="" href="/UserActivity/Create" id="btnCreate">
<span class="glyphicon glyphicon-plus"></span>
</a>
</div>
</div>
<div class="table-responsive" style="margin-top:5px;">
@{
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: @<text>
@Html.Raw("<a data-modal='' href='/UserActivity/Details/" + item.Id + "' id='" + item.Id + "' title='Detail'> <span class='glyphicon glyphicon-search'> </span> </a>")
@Html.Raw("<a data-modal='' href='/UserActivity/edit/" + item.Id + "' id='" + item.Id + "' title='Edit'> <span class='glyphicon glyphicon-edit'> </span> </a>")
@Html.Raw("<a data-modal='' href='/UserActivity/delete/" + item.Id + "' id='" + item.Id + "' title='Delete'> <span class='glyphicon glyphicon-trash'> </span> </a>")
</text>)
));
}
</div>
}
是否有人可以告诉我如何从数据库中检索数据并将其显示在gridview中视图顶部附近的可能重复的put@model列表,然后作为视图中的测试,尝试访问模型以确保它已填充。。。类似@Html.RawModel.EntityName的内容