以下是这个问题的后续:ASP.NET MVC Entity Framework CodeFirst Many To Many (CRUD)
我也有同样的问题。(顺便说一句:在经历了大量ASP.NET WebForms之后,我正在学习MVC )。
(我需要问一系列问题,因为我还没有50名代表来发表评论。)
这个问题的公认答案是添加一行代码“在您的查询代码中”,提问者说这是有效的。
我的创建方法是:
public ActionResult Create() {
db.Students.Include(s => s.Courses);
return View();
}我的created当前是脚手架引擎在创建Controller时生成的:
@model CodeTest.Models.Course
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Course</h4>
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Location, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Location)
@Html.ValidationMessageFor(model => model.Location)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Teacher, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Teacher)
@Html.ValidationMessageFor(model => model.Teacher)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}我的第一个想法是:提问者还做了什么
这就引发了一系列的问题: 1.“查询代码内部”在哪里。它在控制器的create方法中吗?我试过了(见上面),但没有成功。
发布于 2014-04-28 00:46:38
对create视图没有任何影响。
那句话根本说不通。数据上下文不创建cshtml文件。
添加这一行代码后,脚手架屏幕上是否出现了下拉框?需要退货吗?如果是的话,是如何做到的?
不可能回答这个问题,因为没有提供视图代码。此外,您不会将任何数据传递给要使用的视图。
是否还需要向创建和编辑视图中添加其他内容,以允许用户为这许多关系设置一个值?
您需要代码来保存这些值。
https://stackoverflow.com/questions/23330783
复制相似问题