User add date to List MVC 4

Asked

Viewed 137 times

2

I am new to ASP.NET MVC 4 and C# and need help. I have created a Enrollment class (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 there’s 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 Details page where the components appear, and wanted it to be possible to create only the components for this discipline. If you know another way to do it will do. Thank you from now on.

View Index dos 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 Details of Components:

<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>

  • So, about the discipline id, my answer explains. You need to call the Action using a link like http://exemplo:12345/Components/CreateByEnrollmentId/{idDaDisciplina}.

1 answer

2

There are some ways to do it. The most didactic would be to create a Action in ComponentsController to create a discipline component 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 comes with EnrollmentId filled and you can turn the dropdown of disciplines in a @Html.HiddenFor(model => model.EnrollmentId).

Another way is by using the component Begincollectionitem on the creation and editing screens of the disciplines. There are already many answers about that here, then I ask that you see the answers to see if this is what you are looking for.

  • I will test and also see about Begincollectionitem and say something later, thank you. Just one question, within the Enrollments Details view create a link directly to the Components view create? How do I use the id of the chosen discipline to create the component?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.