@model IEnumerable<MvcApplication32.Models.UserInfo> 后台是集合类型了 ,传到前台是model了,用 IEnumerable<>转为集合类型
@model MvcApplication32.Models.UserInfo 强类型
using MvcApplication32.Models; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcApplication32.Controllers { public class HomeController : Controller { // // GET: /Home/ test1Entities db = new test1Entities(); public ActionResult Index() { ViewData["data"]= db.UserInfo.ToList(); return View(); } } }
@model MvcApplication32.Models.UserInfo @using MvcApplication32.Models; @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> </head> <body> <div> @foreach (UserInfo item in ViewData["data"] as List<UserInfo>) { <p>@item.Id</p> <p>@item.UserName</p> <p>@item.Age</p> <hr /> } </div> </body> </html>