1

I've got the usual students courses mvc. What I would like to do is edit the courses in which the student is signed up without entering the courses.

I can display the courses of each student in its detail view, but I am not able sign up new ones with a drop table.

@foreach (var item in Model.Courses)
{
    <tr>
        <td>
            @Html.DropDownList(@*something*@)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.AllCourses) - @Html.DisplayFor(modelItem => item.Courses.Name)
        </td>
    </tr>
}

I would like to be able to add and remove courses from an student without having to enter the actual course.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • Your question isn't clear to me. You want to add a course that isn't in the database to the drop down list of courses? You want to make a relation between a student and a course somehow? Or a combination of those? – Duston Jul 18 '19 at 13:59
  • look at my answer [here](https://stackoverflow.com/questions/57003054/c-sharp-asp-net-add-element-to-a-list-in-a-model-using-form/57015878#57015878) and know if it solves your problem – Bosco Jul 18 '19 at 22:30

1 Answers1

0

The are two ways to Add a course in the details View:

The first one: By using JavaScript and ajax to add a new course

The Second solution: By using ViewModel that contains the details of the the student and a CourseObject

Public Class StudentDetailsVM { 
public Student Student {get;set;}
public Course Course {get;set;}
}

The Student object will contains the details ( Show, Delete ) and you can use the Course object within a form to add course

In ASP.NET Core you can ViewComponent to do this easily

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122