Answer the question
In order to leave comments, you need to log in
Does not output information from the database to the view. ASP.NET MVC. What to do?
I use sql management studio 2016
Here is a screenshot:
Code in View (Index.cshtml):
@model IEnumerable<WebApplication1.Models.Book>
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div>
<h3>Распродажа книг</h3>
<table>
<tr class="header">
<td><p>Название книги</p></td>
<td><p>Автор</p></td>
<td><p>Цена</p></td>
<td></td>
</tr>
@foreach (WebApplication1.Models.Book b in Model)
{
<tr>
<td><p>@b.Name</p></td>
<td><p>@b.Author</p></td>
<td><p>@b.Price</p></td>
<td><p><a href="/Home/Buy/@b.Id">Купить</a></p></td>
</tr>
}
</table>
</div>
<connectionStrings>
<add name="BookContext" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='|DataDirectory|\Bookstore.mdf';Integrated Security=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
BookContext db = new BookContext();
public ActionResult Index()
{
return View(db.Books);
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
public class BookContext : DbContext
{
public BookContext() : base("DefaultConnection")
{ }
public DbSet<Book> Books { get; set; }
public DbSet<Purchase> Purchases { get; set; }
}
Answer the question
In order to leave comments, you need to log in
In connectionStrings you have BookContext , and in the BookContext class you use a completely different connection name - DefaultConnection
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question