1

This is a followup to this question: ASP.NET MVC Entity Framework CodeFirst Many To Many (CRUD)

I have exactly the same problem. (BTW: I am learning MVC after a lot of experience with ASP.NET WebForms)

(I need to ask a series of questions as I don't yet have 50 rep to add a comment.)

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:

@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". Is it inside the create method of the controller? 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?

3
  • 1
    Can you post the full code of the Create method, and the associated View. As it stands, you're not passing a model to the view, so I'd like to see what it is that your view is doing. Commented Apr 28, 2014 at 0:45
  • Thanks Brendan. I have added the create view which is currently simply what was generated when I created the controller with the "MVC5 Controller with views, using Entity Framework" option in Visual Studio 2013. Commented Apr 28, 2014 at 1:58
  • Your model in the view is currently NULL,because you are not passing anything to it. Your Create action needs to retrieve something from the database and then pass it to the view. The code you have currently does not do anything. Commented Apr 28, 2014 at 2:53

1 Answer 1

1

Which did nothing to the create view.

That sentence doesn't even make sense. Data context's don't create cshtml files.

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.