User add date to List MVC 4

2

I'm new to ASP.NET MVC 4 and C # and need help. I created a class Enrollment (discipline)

public class Enrollment
{
    public virtual int EnrollmentId { get; set; }
    public virtual string Name { get; set; }

    public virtual ICollection<Component> Components { get; set; }
}

As you can see, you have a list of components

public class Component
{
    public virtual int ComponentId { get; set; }
    public virtual int EnrollmentId { get; set; }

    public virtual string Name { get; set; }

    public virtual Enrollment Enrollment { get; set; }
}

I created controllers and views using EF. I would like to know how I can get a user to select a discipline that already exists and add components to it. Basically what is done is that a user goes to the index of the disciplines and sees all the disciplines, these disciplines have a link that goes to the page of details where the components appear, and wanted that it was possible to create only the components for this discipline . If they know another way, it will do. Thank you in advance.

View Index of enrollments (disciplines):

    <table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th></th>
    </tr>
  @foreach (var item in Model) {
    <tr>
        <td>
            <a href="@Url.Action("Details", new { id = item.EnrollmentId })">@Html.DisplayFor(modelItem => item.Name)</a>
</td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.EnrollmentId }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.EnrollmentId })
        </td>
    </tr>
}

</table>

View Components Details:

<fieldset>
<div class="display-field">
    <table>
        <tr>
            <th>Name</th>
        </tr>
        @foreach (var item in Model.Components)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Name)
                </td>
            </tr>
        }
    </table>
</div>

    
asked by anonymous 24.04.2016 / 22:02

1 answer

2

There are some ways to do it. The most instructional would be to create an Action in ComponentsController to create a component of the discipline using the Discipline Id. For example:

public ActionResult CreateByEnrollmentId(int id) 
{
    var enrollment = context.Enrollments.FirstOrDefault(e => e.EnrollmentId == id);
    if (enrollment == null)
        return HttpNotFound();

    // Aqui sabemos que a disciplina já existe, então posso montar um
    // objeto com EnrollmentId já definido.

    var component = new Component 
    {
        EnrollmentId = enrollment.EnrollmentId
    }

    return View("Create", component);
}

With this, the Create screen already comes with EnrollmentId completed and you can turn dropdown of subjects into @Html.HiddenFor(model => model.EnrollmentId) .

Another way is to use the BeginCollectionItem component in the creation and editing screens of the courses. There are already many answers about this here , so I ask you to see the answers to see if that is what you are looking for.

    
24.04.2016 / 22:20