我正在使用 MVC 3 编写我的第一个 Internet 应用程序,但是在将数据绑定到 Webgrid 时遇到了问题。我正在使用存储过程提取数据并将其存储在模型的数据表中。我不能直接使用数据表绑定,所以我将它转换为 EnumerableRowCollection,但我似乎无法让它工作。
我已经看到了他们列出模型中每个字段的示例,我认为这是一种非常荒谬的方式。此外,根据他们选择查看的内容和查看的顺序,为此报告返回的字段或动态字段。
在我的模型中:
namespace ShipStatReportsModels.Models
{
public class ShipStatData
{
public EnumerableRowCollection GetReportData()
{
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
try
{
using (SqlConnection _SqlConnection = new SqlConnection(ConfigurationManager.AppSettings["ADONETHorizoninetuser"]))
{
using (SqlCommand cmd = new SqlCommand("UP_ShipStatReports_DataPresentation", _SqlConnection))
{
cmd.CommandTimeout = 120;
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Contact_id", 43135);
cmd.Parameters.AddWithValue("@Contact_Server_ID", 17);
cmd.Parameters.AddWithValue("@Report_Id", 1);
da.SelectCommand = cmd;
da.Fill(dt);
}
}
}
catch (Exception ex)
{
throw ex;
}
return dt.AsEnumerable();
}
}
}
在我的收藏中:
public ActionResult Reports()
{
ShipStatData modelObject = new ShipStatData();
EnumerableRowCollection resultSet = modelObject.GetReportData();
return View(resultSet);
}
在我看来:
@{
ViewBag.Title = "Reports";
}
@model ShipStatReportsModels.Models.ShipStatData
@{
var Grid = new WebGrid(@Model);
}
<h2>Reports</h2>
<div>
@Grid.GetHtml(
tableStyle: "grid",
headerStyle: "head",
alternatingRowStyle: "alt"
)
)
</div>
但我收到以下错误:
编译错误描述:在编译服务此请求所需的资源期间发生错误。请查看以下特定错误详细信息并适当修改您的源代码。
编译器错误消息:CS1502:最佳重载方法匹配 'System.Web.Helpers.WebGrid.WebGrid(System.Collections.Generic.IEnumerable, System.Collections.Generic.IEnumerable, string, int, bool, bool, string, string , string, string, string, string, string)' 有一些无效参数
任何想法,将不胜感激。谢谢!