Asp.net core mvc MVC 6 EF 7属性的列表框未填充
我已经尝试了一段时间,现在得到一个列表框填充,我似乎无法找到它。我已经对EntityFramework7文档进行了相当广泛的研究,但我对它还是新手。MVC6/EF7的教程还不多,因此很难知道将一个实体类与另一个实体类的实例关联的最佳实践是什么。请原谅问题太长,我只是想说得透彻一点 我正在使用EntityFramework7、asp.NET5和MVC6 复制问题的步骤Asp.net core mvc MVC 6 EF 7属性的列表框未填充,asp.net-core-mvc,entity-framework-core,asp.net-mvc-scaffolding,Asp.net Core Mvc,Entity Framework Core,Asp.net Mvc Scaffolding,我已经尝试了一段时间,现在得到一个列表框填充,我似乎无法找到它。我已经对EntityFramework7文档进行了相当广泛的研究,但我对它还是新手。MVC6/EF7的教程还不多,因此很难知道将一个实体类与另一个实体类的实例关联的最佳实践是什么。请原谅问题太长,我只是想说得透彻一点 我正在使用EntityFramework7、asp.NET5和MVC6 复制问题的步骤 创建新的ASP.Net Web应用程序→ 项目名称:ListBox.Web→ 解决方案列表框的名称 选择APS.NET 5模板→
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace ListBox.Web.Models
{
public class Parent
{
public int ParentId { get; set; }
[Required]
public string Name { get; set; }
public ICollection<Child> Children { get; set; }
}
}
\u Layout.cshtml
<ul class="nav navbar-nav">
<li><a asp-controller="Home" asp-action="Index">Home</a></li>
<li><a asp-controller="Parents" asp-action="Index">Parents</a></li>
<li><a asp-controller="Children" asp-action="Index">Children</a></li>
<li><a asp-controller="Home" asp-action="About">About</a></li>
<li><a asp-controller="Home" asp-action="Contact">Contact</a></li>
</ul>
- 将显示一个父级下拉框,但下拉框中没有可选择的项目
- 列表框的HTML为:
使用System.Linq;
使用Microsoft.AspNet.Mvc;
使用Microsoft.AspNet.Mvc.Rendering;
使用Microsoft.Data.Entity;
使用ListBox.Web.Models;
命名空间ListBox.Web.Controllers
{
公共类子控制器:控制器
{
私有应用程序的bContext\u上下文;
公共子控制器(ApplicationDbContext上下文)
{
_上下文=上下文;
}
//获得:儿童
公共IActionResult索引()
{
var applicationDbContext=_context.Child.Include(c=>c.Parent);
返回视图(applicationDbContext.ToList());
}
//获取:儿童/详细信息/5
公共IActionResult详细信息(int?id)
{
if(id==null)
{
返回HttpNotFound();
}
Child=\u context.Child.Single(m=>m.ChildId==id);
if(child==null)
{
返回HttpNotFound();
}
返回视图(子视图);
}
//获取:子对象/创建
public IActionResult Create()
{
ViewData[“ParentId”]=新的选择列表(_context.Set(),“ParentId”,“Parent”);
返回视图();
}
//职位:儿童/创建
[HttpPost]
[ValidateAntiForgeryToken]
公共IActionResult创建(子项)
{
if(ModelState.IsValid)
{
_context.Child.Add(Child);
_SaveChanges();
返回操作(“索引”);
}
ViewData[“ParentId”]=新的选择列表(_context.Set(),“ParentId”,“Parent”,child.ParentId);
返回视图(子视图);
}
//获取:Children/Edit/5
公共IActionResult编辑(int?id)
{
if(id==null)
{
返回HttpNotFound();
}
Child=\u context.Child.Single(m=>m.ChildId==id);
if(child==null)
{
返回HttpNotFound();
}
ViewData[“ParentId”]=新的选择列表(_context.Set(),“ParentId”,“Parent”,child.ParentId);
返回视图(子视图);
}
//职位:儿童/编辑/5
[HttpPost]
[ValidateAntiForgeryToken]
公共IActionResult编辑(子项)
{
if(ModelState.IsValid)
{
_更新(儿童);
_SaveChanges();
返回操作(“索引”);
}
ViewData[“ParentId”]=新的选择列表(_context.Set(),“ParentId”,“Parent”,child.ParentId);
返回视图(子视图);
}
//获取:Children/Delete/5
[行动名称(“删除”)]
公共IActionResult删除(int?id)
{
if(id==null)
{
返回HttpNotFound();
}
Child=\u context.Child.Single(m=>m.ChildId==id);
if(child==null)
{
返回HttpNotFound();
}
返回视图(子视图);
}
//帖子:Children/Delete/5
[HttpPost,ActionName(“删除”)]
[ValidateAntiForgeryToken]
公共IActionResult DELETE已确认(内部id)
{
Child=\u context.Child.Single(m=>m.ChildId==id);
_context.Child.Remove(Child);
_SaveChanges();
返回操作(“索引”);
}
}
}
Child Create.cshtml
@model ListBox.Web.Models.Child
@{
ViewData[“标题”]=“创建”;
}
创造
小孩
返回列表
@节脚本{
}
更改创建()
子控制器中的方法,更改
public IActionResult Create()
{
ViewData["ParentId"] = new SelectList(_context.Set<Parent>(), "ParentId", "Parent");
return View();
}
<select asp-for="ParentId" class="form-control"></select>
到
生成的代码不正确,这是一个bug 使用“标记助手”的一种解决方案是: 控制器
...
ViewData["Parents"] = new SelectList(_context.Set<Parent>(), "ParentId", "Name", child.ParentId);
...
。。。
ViewData[“Parents”]=新的选择列表(_context.Set(),“ParentId”,“Name”,child.ParentId);
...
查看
@{
var parents = (IEnumerable<SelectListItem>)ViewData["Parents"];
}
...
<select asp-for="ParentId" asp-items="parents" class ="form-control">
<option disabled selected>--- SELECT ---</option>
</select>
...
@{
var parents=(IEnumerable)ViewData[“parents”];
}
...
---挑选---
...
当只有一种类型的对象嵌套在同一类型的另一个对象中时,如何执行此操作
对象:
公共级机队
{
公共int Id{get;set;}
公共车队父车队{get;set;}
public int?ParentFleetId{get;set;}
public IActionResult Create()
{
ViewData["ParentId"] = new SelectList(_context.Set<Parent>(), "ParentId", "Parent");
return View();
}
public IActionResult Create()
{
ViewData["ParentId"] = new SelectList(_context.Set<Parent>(), "ParentId", "Name");
return View();
}
<select asp-for="ParentId" class="form-control"></select>
@Html.DropDownList("ParentId", null, htmlAttributes: new { @class = "form-control" })
...
ViewData["Parents"] = new SelectList(_context.Set<Parent>(), "ParentId", "Name", child.ParentId);
...
@{
var parents = (IEnumerable<SelectListItem>)ViewData["Parents"];
}
...
<select asp-for="ParentId" asp-items="parents" class ="form-control">
<option disabled selected>--- SELECT ---</option>
</select>
...
ViewData["ParentFleetId"] = new SelectList(_context.Set<Fleet>(), "Id", "Name");
return View();