我想显示数据库中的所有数据。
但它不起作用。 我的Web.config文件
<connectionStrings>
<add name="EFDbContext" providerName="System.Data.SqlClient"
connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=NewsList;Integrated Security=True"/>
控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NewsList.Domain.Abstract;
using NewsList.Domain.Entities;
namespace NewsList.WebUI1.Controllers
{
public class NewsController : Controller
{
//
// GET: /News/
private INewsRepository repository;
public NewsController(INewsRepository newsRepository)
{
this.repository = newsRepository;
}
public ViewResult List()
{
return View(repository.NewsList);
}
public ActionResult Index()
{
return View();
}
}
}
模型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NewsList.Domain.Entities
{
public class News
{
public int NewsID { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Text { get; set; }
}
}
查看
@model IEnumerable<NewsList.Domain.Entities.News>
@{
ViewBag.Title = "News";
}
@foreach (var p in Model)
{
<div class="item">
<h3>@p.Title</h3>
@p.Description
<h4>@p.Text</h4>
</div>
}
现在我还没有得到任何惊吓。仅在浏览器页面清除。
答案 0 :(得分:0)
仅作为一个例子:
public class NewsRepository : INewsRepository
{
private YourDbContext db = null;
public List<NewsList> NewsList
{
return this.db.NewsList.ToList();
}
public NewsRepository()
{
this.db = new NewsRepository();
}
}
public class YourDbContext : DbContext
{
public DbSet<NewsList> NewsList {get; set;}
}