Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework .Net Core如何为关系创建视图_Entity Framework_Asp.net Core_.net Core - Fatal编程技术网

Entity framework .Net Core如何为关系创建视图

Entity framework .Net Core如何为关系创建视图,entity-framework,asp.net-core,.net-core,Entity Framework,Asp.net Core,.net Core,嗨,我用这种方法建立了一个简单的多对多模型 public class Book { public int Id { get; set; } public string BookName { get; set; } public string ISBN { get; set; } public IList<BookAuthor> BookAuthor { get; set; }

嗨,我用这种方法建立了一个简单的多对多模型

   public class Book
    {
      
        public int Id { get; set; }
       
        public string BookName { get; set; }
        public string ISBN { get; set; }

        public IList<BookAuthor> BookAuthor { get; set; }

    }

 public class Author
    {
        public int Id { get; set; }
        public string AuthorName { get; set; }

        public IList<BookAuthor> BookAuthor { get; set; }

    }



public class BookAuthor
    {
        public int BookId { get; set; }
        public Book Book { get; set; }

        public int AuthorId { get; set; }
        public Author Author { get; set; }
    }
公共课堂教材
{
公共int Id{get;set;}
公共字符串BookName{get;set;}
公共字符串ISBN{get;set;}
公共IList BookAuthor{get;set;}
}
公共类作者
{
公共int Id{get;set;}
公共字符串AuthorName{get;set;}
公共IList BookAuthor{get;set;}
}
公共类图书作者
{
public int BookId{get;set;}
公共图书{get;set;}
公共int AuthorId{get;set;}
公共作者{get;set;}
}
也在上下文中

  public DbSet<Book> Books { get; set; }
        public DbSet<Author> Authors { get; set; }
        public DbSet<BookAuthor> BookAuthor { get; set; }
公共数据库集书籍{get;set;}
公共数据库集作者{get;set;}
公共数据库集BookAuthor{get;set;}
它用一个桥接表在db中创建了3个表。我只想为多对多关系创建视图。 在我的故事里,一本书可以有许多作者。一个作家可以有很多书

我为Book和Author模型创建了一个带有视图的ef控制器,它只创建了带有一个输入区域的视图 此外,我还为bridgemodel(BookAuthor)创建了ef控制器,它使用2个下拉按钮创建视图,其中只有ID 如何为其创建视图。 多谢各位

如何为关系创建视图

首先,在控制器中,应该使用
Include
Include
方法从数据库中加载相关实体,代码如下:

 _dbcontext.Books.Include(c => c.BookAuthor).ThenInclude(c => c.Author);
    @model IEnumerable<WebApplication2.Models.Book>

    @{
        ViewData["Title"] = "BookIndex";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }

    <h1>BookIndex</h1>

    <p>
        <a asp-action="Create">Create New</a>
    </p>
    <table class="table">
        <thead>
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.Id)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.BookName)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.ISBN)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.BookAuthor)
                </th>
                <th></th>
            </tr>
        </thead>
        <tbody>
    @foreach (var item in Model)
    {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Id)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.BookName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.ISBN)
                </td>
                <td>
                   @{ var authorlist = new List<string>();
                       foreach (var author in item.BookAuthor)
                       {
                           authorlist.Add(author.Author.AuthorName);
                       }
                       @string.Join(",", authorlist);
                   }
                </td>
                <td>
                    @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
                    @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
                    @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
                </td>
            </tr>
    }
        </tbody>
    </table>
然后,将查询结果返回到查看页面。在查看页面中,您可以使用
foreach
语句循环浏览BookAuthor列表并获取相关实体,如下所示:

 _dbcontext.Books.Include(c => c.BookAuthor).ThenInclude(c => c.Author);
    @model IEnumerable<WebApplication2.Models.Book>

    @{
        ViewData["Title"] = "BookIndex";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }

    <h1>BookIndex</h1>

    <p>
        <a asp-action="Create">Create New</a>
    </p>
    <table class="table">
        <thead>
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.Id)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.BookName)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.ISBN)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.BookAuthor)
                </th>
                <th></th>
            </tr>
        </thead>
        <tbody>
    @foreach (var item in Model)
    {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Id)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.BookName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.ISBN)
                </td>
                <td>
                   @{ var authorlist = new List<string>();
                       foreach (var author in item.BookAuthor)
                       {
                           authorlist.Add(author.Author.AuthorName);
                       }
                       @string.Join(",", authorlist);
                   }
                </td>
                <td>
                    @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
                    @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
                    @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
                </td>
            </tr>
    }
        </tbody>
    </table>
查看页面中的代码:

@model WebApplication2.Models.BookViewModel

@{
    ViewData["Title"] = "AddBook";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h1>AddBook</h1>

<h4>BookViewModel</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="AddBook">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="BookName" class="control-label"></label>
                <input asp-for="BookName" class="form-control" />
                <span asp-validation-for="BookName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="ISBN" class="control-label"></label>
                <input asp-for="ISBN" class="form-control" />
                <span asp-validation-for="ISBN" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="AuthorName" class="control-label"></label>
                <input asp-for="AuthorName" class="form-control" />
                <span asp-validation-for="AuthorName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>
@model WebApplication2.Models.BookViewModel
@{
ViewData[“Title”]=“AddBook”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
地址簿
BookViewModel

控制器中的代码:

    public IActionResult AddBook()
    {

        return View();                 
    }
    [HttpPost]
    public IActionResult AddBook(BookViewModel bookViewModel)
    {
        if (ModelState.IsValid)
        {
            var newbook = new Book() { BookName = bookViewModel.BookName, ISBN = bookViewModel.ISBN};

            string[] author = bookViewModel.AuthorName.Split(','); //if have multiple author, using "," separate them.
            //List<Author> authors = new List<Author>();
            List<BookAuthor> bookAuthors = new List<BookAuthor>();
            List<Author> newauthors = new List<Author>();
            foreach (var item in author)
            {
               var existauthor = _dbcontext.Authors.Where(c => c.AuthorName.ToLower() == item.ToLower()).FirstOrDefault();
                if (existauthor == null)
                {
                    existauthor = new Author() { AuthorName = item };
                    newauthors.Add(existauthor);
                }
                //authors.Add(existauthor);
                bookAuthors.Add(new BookAuthor() { Author = existauthor, Book = newbook });
            }

            _dbcontext.Authors.AddRange(newauthors);
            _dbcontext.Books.AddRange(newbook);
            _dbcontext.BookAuthors.AddRange(bookAuthors);

            _dbcontext.SaveChanges();
            return RedirectToAction(nameof(BookIndex));
        }
        return View();
    }
public IActionResult AddBook()
{
返回视图();
}
[HttpPost]
公共IActionResult AddBook(BookViewModel BookViewModel)
{
if(ModelState.IsValid)
{
var newbook=newbook(){BookName=bookViewModel.BookName,ISBN=bookViewModel.ISBN};
string[]author=bookViewModel.AuthorName.Split(',');//如果有多个作者,请使用“,”分隔它们。
//列表作者=新列表();
List bookAuthors=新列表();
List newauthors=newlist();
foreach(作者中的变量项)
{
var existauthor=_dbcontext.Authors.Where(c=>c.AuthorName.ToLower()==item.ToLower()).FirstOrDefault();
if(existauthor==null)
{
existauthor=new Author(){AuthorName=item};
添加(existauthor);
}
//作者。添加(existauthor);
添加(newbookauthor(){Author=existauthor,Book=newbook});
}
_dbcontext.Authors.AddRange(newauthors);
_dbcontext.Books.AddRange(newbook);
_dbcontext.BookAuthors.AddRange(BookAuthors);
_dbcontext.SaveChanges();
返回重定向到操作(名称(BookIndex));
}
返回视图();
}

我只想为2个相关实体创建一个crud页面谢谢您,它将被使用