I have a mvc controller name "Usuarios" which index display 3 columns of the Usuarios table in my DB, because of having relations problems in the database, we created a relational table between "Usuarios" and "Cursos" (1 "usuarios" can have N "curos" and the other way around). So now I'm trying to display the "cursos" name (which is related with "usuarios" by the relational table) on the "Usuarios" index.html.
How can I do this?
This is my index.cshtml code and in the last TD I would like to add the Cursos.name which is in another table that haves a relational table with the "Usuarios" table.
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Nombre)
</td>
<td>
#Html.DisplayFor(modelItem => item.Apellido)
</td>
<td>
#Html.DisplayFor(modelItem => item.DNI)
</td>
<td>
#Html.DisplayFor()
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.idUsuario}) |
#Html.ActionLink("Details", "Details", new { id=item.idUsuario }) |
#Html.ActionLink("Delete", "Delete", new { id=item.idUsuario })
</td>
</tr>
Use include when get data from database.
.Include("Cursos")
Then use
#Html.DisplayFor(modelItem => item.Cursos.Nombre)
Related
Little context:
I have a link model which has a foreign Key ID to the identifier model and client model.
The client model and identifier model have the link model as a virtual Icollection.
Both the Client model and the identifier model have a foreign Key ID to another model MJTopics.
I'm using EF6 to create controllers with views and on the details view of the Client controller I can pull information from the links icollection into the view however I want to pull in identifiers which share the same MJTopicsID as the Client as shown below:
Use of the Links Icollection in Client Details:
<h3>Links Built</h3>
<table class="table">
<tr>
<th>Domain</th>
<th>Page Containing Link</th>
<th>Anchor Text</th>
<th>Date</th>
<th>Action</th>
</tr>
#foreach (var item in Model.Links)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Identifier.domain)
</td>
<td>
#Html.DisplayFor(modelItem => item.Obdomain)
</td>
<td>
#Html.DisplayFor(modelItem => item.Anchor)
</td>
<td>
#Html.DisplayFor(modelItem => item.BuildDate)
</td>
<td>
#Html.ActionLink("Edit", "Edit", "Status", new { id=item.LinkID }) |
#Html.ActionLink("Domain", "Details", "Status", new { id=item.LinkID }) |
#Html.ActionLink("Delete", "Delete", "Status", new { id=item.LinkID })
</td>
</tr>
}
</table>
Possible related domains (I set a viewbag variable for the MJTopics ID of the client):
<h3>Possible Related Domains</h3>
<table class="table">
<tr>
<th>Domain</th>
<th>Status</th>
<th>Date</th>
<th>Action</th>
</tr>
#foreach (var item in Model.Identifier.Where(p=>p.MJTopics.ID == ViewBag.MjTopicsID))
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Identifier.domain)
</td>
<td>
#Html.DisplayFor(modelItem => item.status)
</td>
<td>
#Html.DisplayFor(modelItem => item.Last)
</td>
<td>
#Html.ActionLink("Go Live", "Live", "Status", new { id=item.StatusID }) |
#Html.ActionLink("Edit", "Edit", "Status", new { id=item.StatusID }) |
#Html.ActionLink("Domain", "Details", "Status", new { id=item.StatusID }) |
#Html.ActionLink("Delete", "Delete", "Status", new { id=item.StatusID })
</td>
</tr>
}
</table>
How would I go about doing this? I tried specifying another model in parenthesis but obviously it only allows one model per view, the only other way I have read about is partial views but don't know anything about these as of yet. Is there another simple way?
This was achieved using a ViewModel!
In default MVC project VS2013, when the data is displayed in table there are options for Edit | Delete | Details. When the Edit link is clicked, the row of data is rendered on another page for Editing.
But I've to change the label to textbox when the #Html.ActionLink for Edit is clicked i.e change #Html.DisplayFor to #Html.EditorFor . How can I do that? I am trying to edit the the data in the row without rendering to another page.
//<table class="table">
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.student)
</td>
<td>
#Html.DisplayFor(modelItem => item.course)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.id }) |
#Html.ActionLink("Details", "Details", new { id = item.id }) |
#Html.ActionLink("Delete", "Delete", new { id = item.id })
</td>
</tr>
}
I'm using default schema of Create View and in my model i've got a field that's a DateTime.
Now i'm doing that, cause if i use the #Html.DisplayFor(modelItem => item.BirthDate) i see a string formatted in this way 13/02/1989 00:00:00 instead of this 16/02/1989
First of All: How to make all with DisplayFor statement? And... How it Works with datatype, it's a good practice?
Next: I wonder i have some homework to do, some suggestion?
#foreach (var item in Model)
{
<tr class="odd gradeX">
<td>
<input name="UserIDs" type="checkbox" class="checkboxes" value=#item.Id />
</td>
<td>
#Html.DisplayFor(modelItem => item.FirstName) #Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Email)
</td>
<td>
#Html.DisplayFor(modelItem => item.Phone)
</td>
<td>
#Html.DisplayFor(modelItem => item.Fax)
</td>
<td>
#item.BirthDate.ToShortDateString()
</td>
<td>
#Html.ActionLink("Details", "Details", new { Id = item.Id })
</td>
</tr>
}
Add this using statement to your model:
using System.ComponentModel.DataAnnotations;
Then add this attribute to your BirthDate field:
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy'-'MM'-'dd}")]
Edit as necessary.
I have a list of Topic's. Each Topic have a list of Message. When i click on "Details" I want to open a new View that display a list of the Message's from the Topic.
I expect that I need to modify this line: (this is what I have so far)
#Html.ActionLink("Details", "Details", new { id=item.Id }, null)
The Message's are accessed by item.Message.
And the first line in the Detail View
#model IEnumerable<Models.TopicMessage>
This is my Topic View:
#model IEnumerable<Models.Topic>
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.Name)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.ActionLink("Details", "Details", new { id=item.Id }, null)
</td>
</tr>
}
</table>
There are a number of ways to do that. You can use the Action method to execute the controller action directly:
<tr>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.Action("Details", "Details", new { id=item.Id })
</td>
</tr>
Or you can bypass the controller and render the view directly using this overload of DisplayFor:
<tr>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Messages, "MessagesDetail")
</td>
</tr>
Or if you define the template as the default DisplayTemplate, you can just do this:
<tr>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Messages)
</td>
</tr>
I fixed this by making a somethingViewModel that contained both the liste and the Id. That way i could contain the two information from two difference places in one new object I had access to in the View.
I pass my model to my view. On this view I am displaying a table and trying to populate it with a list of information from the database.
I am using asp.net-mvc, entity framework, razor, c# and LINQ
However I need it grouped by a certain column kpiID.
Here are all of the columns in the table.
kpiHistoryID
kpiID
startAmount
completedamount
endAmount
completeBy
comments
The kpiID links back the the KPI table and is where I can grab the actual name of the KPI.
How exactly do I group the data and can it be done in the below #foreach line?
<tbody>
#foreach (var item in #Model.IPACS_Department.IPACS_Functions.SelectMany(m => m.IPACS_Processes).SelectMany(m => m.IPACS_Procedures).SelectMany(m => m.IPACS_KPIS).SelectMany(m => m.IPACS_kpiHistory))
{
<tr class="gradeX">
<td>
#item.IPACS_KPIS.name
</td>
<td>
#item.startAmount
</td>
<td>
#item.completedAmount
</td>
<td>
#item.endAmount
</td>
</tr>
}
</tbody>
This:
#Model.IPACS_Department.IPACS_Functions.SelectMany(m => m.IPACS_Processes)
.SelectMany(m => m.IPACS_Procedures).SelectMany(m => m.IPACS_KPIS)
.SelectMany(m => m.IPACS_kpiHistory)
gives a list of denormalised kpiHistories, yes? Does it not give you the answer you want if you do
thatList.GroupBy(x => x.kpiID)
Only way I could figure out how to do it was using sum. Couldn't get it to work with group by at all.
<tbody>
#foreach (var item in #Model.IPACS_Department.IPACS_Functions.SelectMany(m => m.IPACS_Processes).SelectMany(m => m.IPACS_Procedures).SelectMany(m => m.IPACS_KPIS))
{
<tr class="gradeX">
<td>
#item.name
</td>
<td>
#item.IPACS_kpiHistory.Select(m => m.startAmount).Sum()
</td>
<td>
#item.IPACS_kpiHistory.Select(m => m.completedAmount).Sum()
</td>
<td>
#item.IPACS_kpiHistory.Select(m => m.endAmount).Sum()
</td>
</tr>
}
</tbody>