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 likehttp://exemplo:12345/Components/CreateByEnrollmentId/{idDaDisciplina}
.– Leonel Sanches da Silva