A
A
Andrey Romanyuk2017-02-05 19:02:35
ASP.NET
Andrey Romanyuk, 2017-02-05 19:02:35

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:
b8623eaf4ec640a9965dcf9699efc69c.JPGCode 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>

webconfig:
<connectionStrings>
    <add name="BookContext" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='|DataDirectory|\Bookstore.mdf';Integrated Security=True"
 providerName="System.Data.SqlClient"/>
  </connectionStrings>

HomeController:
BookContext db = new BookContext();

    public ActionResult Index()
    {
        return View(db.Books);
    }
    protected override void Dispose(bool disposing)
    {
        db.Dispose();
        base.Dispose(disposing);
    }

BookContext:
public class BookContext : DbContext
{
    public BookContext() : base("DefaultConnection")
    { }
    public DbSet<Book> Books { get; set; }
    public DbSet<Purchase> Purchases { get; set; }

}

There is data in the DB:

65de12527c754cfb8e3fbcc910724701.JPG Is it possible that the database is turned off after the start? :
c7de499564364c02b1b4fb8f5b767557.JPG
With connectionString, I have no suspicions.... There is
a Book class with data.
In general, I did everything, as in this book: metanit.com/sharp/mvc5/5.1.php

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
Zelimkhan Beltoev, 2017-02-05
@BLek2

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 question

Ask a Question

731 491 924 answers to any question