获取此错误我正在使用ASP.NET MVC在网格中显示记录 [英] Getting this error I am displaying records in grid using ASP.NET MVC
本文介绍了获取此错误我正在使用ASP.NET MVC在网格中显示记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[WebApplication2.personaldetail]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[WebApplication2.Models.personaldetails]'.
我尝试过:
@model IEnumerable< webapplication2.models.personaldetails>
What I have tried:
@model IEnumerable<webapplication2.models.personaldetails>
推荐答案
你应该使用model
键入Type
从Action
返回。
You should use themodel
type as theType
returning from theAction
.
@model IEnumerable< WebApplication2.Models.personaldetails>
@model List< WebApplication2.Models.personaldetails>
< br $> b $ b或
or
var data = obj.personaldetails.ToList();
var data = obj.personaldetails.AsEnumerable();
using System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
使用WebApplication2.Controllers;
命名空间WebApplication2.Controllers
{
公共类demoController:Controller
{
detailsEntities1 obj = new detailsEntities1();
public ActionResult Index()
{
返回查看();
}
公共ActionR esult webgrid()
{
var data = obj.personaldetails.ToList();
return View(data);
}
}
---- -------------------------------------------------
@model IEnumerable< WebApplication2.Models.personaldetails>
@ {
ViewBag.Title =webgrid;
WebGrid网格= new WebGrid(Model,canSort:false,defaultSort:firstname,rowsPerPage:3,
columnNames:new [] {firstname,lastname});
}
@ grid.GetHtml(列:grid.Columns(grid.Column(firstname,header:firstname),
grid.Column( lastname,lastname),
grid.Column(id,id),
grid.Column(active,active)) )
webgrid
------------------------- ----------
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication2.Controllers;
namespace WebApplication2.Controllers
{
public class demoController : Controller
{
detailsEntities1 obj = new detailsEntities1();
public ActionResult Index()
{
return View();
}
public ActionResult webgrid()
{
var data = obj.personaldetails.ToList();
return View(data);
}
}
-----------------------------------------------------
@model IEnumerable< WebApplication2.Models.personaldetails>
@{
ViewBag.Title = "webgrid";
WebGrid grid = new WebGrid(Model,canSort:false,defaultSort:"firstname",rowsPerPage:3,
columnNames:new[]{"firstname","lastname"});
}
@grid.GetHtml(columns:grid.Columns(grid.Column("firstname",header:"firstname"),
grid.Column("lastname","lastname"),
grid.Column("id","id"),
grid.Column("active","active")))
webgrid
-----------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication2.Models
{
public class personaldetails
{
public string id { set; get; }
public string firstname { set; get; }
public string lastname { set; get; }
public string active { set; get; }
}
}
这篇关于获取此错误我正在使用ASP.NET MVC在网格中显示记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文