B. Zab B. Zab - 29 days ago 7
C# Question

Show only unique Id's in View ASP.NET MVC

I'm having a trouble with my view. Currently I'm creating a form. It's a model that has three one to many references and basically stores data from three models.
Here's how my form looks

As you can see the data like commision name and commission description I don't need to duplicate. I need a way in razor code to post only unique data names and description, but the members and positions table should be showed the same way.

I can't find a way, I tried many things but It didn't help.

Here's my Razor View code as of now

@model IEnumerable<CPO.Models.CommissionEmployeePosition>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}


<h2>Commissions</h2>

<p>
@Html.ActionLink("Create New", "Create")
</p>

<table class="table table-bordered table-hover" >
<tr>
<th>Commission</th>
<th>Description</th>
<th>Members</th>
<th>Positions</th>
<th>Feedback</th>
<th>Actions</th>
</tr>
@foreach (var item in Model.OrderBy(m=>m.CommissionId))
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Commission.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Commission.Description)
</td>
<td>
@Html.DisplayFor(modelItem => item.Employee.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.EmployeePosition.Name)
</td>
<td>
<p>@Html.ActionLink("List", "Index", "Annotation", new { id =
item.Commission.Id }, null)</p>
<p>@Html.ActionLink("New", "Create", "Annotation", new { id =
item.Commission.Id }, null)</p>
</td>
<td>
<p>@Html.ActionLink("Delete Commission", "Delete", "Commission", new
{ id = item.Commission.Id }, null)</p>
</td>
</tr>
}

</table>


Any help is highly appreciated. If needed, I can insert my database, how it looks and other things, but as of now I don't think it is needed (I might be highly wrong)

Edit:
My Action that inserts values to this database

foreach (var employee in model.Employees)
{
if (employee.Selected == true)
{
var commissionemployeeposition = new CommissionEmployeePosition();
commissionemployeeposition.CommissionId = commission.Id;
commissionemployeeposition.EmployeeId = employee.Id;
if (model.SupervisorID == commissionemployeeposition.EmployeeId)
commissionemployeeposition.EmployeePositionId = (int)Position.Supervisor;
else
commissionemployeeposition.EmployeePositionId = (int)Position.Member;

//commission.CommissionEmployeePositions.Add(commissionemployeeposition);
unitOfWork.CommissionEmployeePositionRepository.Insert(commissionemployeeposition);
}
}


Edit: I added my Model

public class CommissionEmployeePosition
{
public int CommissionId { get; set; }
public int EmployeeId { get; set; }
public int EmployeePositionId { get; set; }
public Commission Commission { get; set; }
public Employee Employee { get; set; }
public EmployeePosition EmployeePosition { get; set; }
}

Answer Source

Are you trying to remove items that have duplicate Name and Description? That's what I gathered from the question, so sorry if that's incorrect.

You should be able to do something like this in your Razor syntax...

EDIT: Name and Description are of children objects.

@foreach (var item in Model.OrderBy(m => m.CommissionId).GroupBy(m => new { m.Commission.Name, m.Commission.Description }).Select(group => group.First()))