我想在webgrid中显示来自db的车辆列表。但是当我运行代码时,view.chstml中出现“必须绑定数据源”的错误,尽管我使用了正确的型号名称和数据库名称。
型号:车辆
public List<Vehicles> Listvehicle(string uid)
{
List<Vehicles> svehicle;
using (var session = MvcApplication.Store.OpenSession())
{
svehicle = (from vehc in session.Query<Vehicles>() where vehc.userid == uid select vehc).ToList();
}
return svehicle;
}
控制器
[HttpGet]
public ActionResult Listvehilce(string uid)
{
string userName = "ksundas7@gmail.com";
Register reg = new Register();
string userId = reg.userid(userName);
Vehicles vehcl = new Vehicles();
List<Vehicles> vehclist = vehcl.Listvehicle(userId);
vehclist.ToList();
// List<Vehicles> svehicle = WebGrid.Listvehicle(userId);
return View();
}
查看
@model IEnumerable<MvcMembership.Models.Vehicles>
@{
ViewBag.Title = "Listvehicle";
}
<h2>Listvehilce</h2>
@{
var grid = new WebGrid(source: Model, canPage: true, rowsPerPage: 3, selectionFieldName: "selectedRow");
grid.Pager(WebGridPagerModes.NextPrevious);}
<style type="text/css">
.table { margin: 4px; width: 500px; background-color:#FCFCFC;}
.head { background-color: #C1D4E6; font-weight: bold; color: #FFF; }
.webGrid th, .webGrid td { border: 1px solid #C0C0C0; padding: 5px; }
.altRow { background-color: #E4E9F5; color: #000; }
.gridHead a:hover {text-decoration:underline;}
.description { width:auto}
.selectRow{background-color: #389DF5}
</style>
@grid.GetHtml(tableStyle: "grid",
headerStyle: "head",
alternatingRowStyle: "alt",
selectedRowStyle: "select",
columns: grid.Columns(
grid.Column("vehicleid"),// format: (item) => item.GetSelectLink(item.vehicleid)),
grid.Column("Type", " Type"),
grid.Column("Make", "Make"),
grid.Column("Registration", "Registration"),
grid.Column("Colour", "Colour"),
grid.Column("Model", "Model")
))
错误: 必须先绑定数据源才能执行此操作。
任何人都可以告诉我这个错误的原因是什么。 提前致谢 此致
答案 0 :(得分:0)
在绑定到webgrid时,您是否检查过该模型是否为空?
此外,您可以在控制器中更改此部分:
List<Vehicles> vehclist = vehcl.Listvehicle(userId);
vehclist.ToList();
有了这个 IEnumerable vehclist = vehcl.Listvehicle(userId);
List已经实现了IEnumerable接口。
答案 1 :(得分:0)
您没有在视图中返回列表。这里有更新的代码。
[HttpGet]
public ActionResult Listvehilce(string uid)
{
string userName = "ksundas7@gmail.com";
Register reg = new Register();
string userId = reg.userid(userName);
Vehicles vehcl = new Vehicles();
List<Vehicles> vehclist = vehcl.Listvehicle(userId);
vehclist.ToList();
// List<Vehicles> svehicle = WebGrid.Listvehicle(userId);
return View(svehicle);
}