简体   繁体   English

ASP.NET MVC实体框架CodeFirst多对多(CRUD)跟踪

[英]ASP.NET MVC Entity Framework CodeFirst Many To Many (CRUD) Follow Up

This is a followup to this question: ASP.NET MVC Entity Framework CodeFirst Many To Many (CRUD) 这是此问题的后续措施: ASP.NET MVC实体框架CodeFirst多对多(CRUD)

I have exactly the same problem. 我也有完全一样的问题。 (BTW: I am learning MVC after a lot of experience with ASP.NET WebForms) (顺便说一句:我在使用ASP.NET WebForms的丰富经验之后正在学习MVC)

(I need to ask a series of questions as I don't yet have 50 rep to add a comment.) (我需要提出一系列问题,因为我还没有50名代表来添加评论。)

The accepted answer to that question says to add one line of code "inside your query code" to which the questioner said it worked. 对该问题的可接受答案是,在发问者认为对其起作用的“查询代码内”添加一行代码。

My Create method is: 我的创建方法是:

public ActionResult Create() {
    db.Students.Include(s => s.Courses);
    return View();
}

My Create View is currently what has been generated by the scaffolding engine when I created the Controller: 我的创建视图当前是我创建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")
}

My first thought is: what else did the questioner do so it "just worked"? 我的第一个想法是:发问者还做了什么,使它“起作用”?

This raises a series of questions: 1. Where is "inside the query code". 这就提出了一系列问题:1.“查询代码内部”在哪里。 Is it inside the create method of the controller? 它在控制器的create方法内吗? I tried that (see above) and it didn't work. 我试过了(见上文),但没有用。

  1. Did a dropdown box appear on the scaffolding screen by adding this one line of code? 通过添加这一行代码,在脚手架屏幕上是否出现了一个下拉框? Was rescaffolding required? 需要重新折叠吗? If so, how is that done? 如果是这样,那怎么办?

  2. Was anything else required to be added to the create and edit views to allow the user to set a value for this many to many relationship? 是否需要将其他任何内容添加到创建和编辑视图中,以允许用户为这种多对多关系设置值?

Which did nothing to the create view. 这对创建视图没有任何作用。

That sentence doesn't even make sense. 这个句子甚至没有意义。 Data context's don't create cshtml files. 数据上下文不会创建cshtml文件。

Did a dropdown box appear on the scaffolding screen by adding this one line of code? 通过添加这一行代码,在脚手架屏幕上是否出现了一个下拉框? Was rescaffolding required? 需要重新折叠吗? If so, how is that done? 如果是这样,那怎么办?

It's not possible to answer this question, as there is no view code provided. 由于未提供查看代码,因此无法回答此问题。 Additionally, you aren't passing any data to the view to consume. 此外,您不会将任何数据传递给视图以使用。

Was anything else required to be added to the create and edit views to allow the user to set a value for this many to many relationship? 是否需要将其他任何内容添加到创建和编辑视图中,以允许用户为这种多对多关系设置值?

You'll need code to save those values. 您将需要代码来保存这些值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM