.NET Using Another Unrelated Model In Razor View - c#

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!

Related

When I create this List using T4 Templete it gives me a exeption NullReferenceException

I was trying to make a list of all the users and I tried this code and this one is giving me an exception of NullReferenceException I tried to type it manually and then when it was making trouble I tried to scaffold it using List T4 Template but it still didnt work out
#model IEnumerable<Aroma.Models.user>
#{
ViewBag.Title = "ManageUsers";
}
<h2>ManageUsers</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.username)
</th>
<th>
#Html.DisplayNameFor(model => model.pass)
</th>
<th>
#Html.DisplayNameFor(model => model.email)
</th>
<th>
#Html.DisplayNameFor(model => model.usertype.name)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.username)
</td>
<td>
#Html.DisplayFor(modelItem => item.pass)
</td>
<td>
#Html.DisplayFor(modelItem => item.email)
</td>
<td>
#Html.DisplayFor(modelItem => item.usertype.name)
</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>
}
</table>
You are trying to display the name of the property on the foreach iteration. That's probably it, since I dont think it has the necessary Attribute to do so.
You should just do:
#foreach (var item in Model) {
<tr>
<td>
#item.username
</td>
<td>
#item.pass
</td>
<td>
#item.email
</td>
<td>
#item.usertype.name
</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>
}

Display a relationship column in HTML with mvc

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)

How to made item.Value with DisplayFor() ?

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.

How do i show a list from a model to another View in ASP.NET MVC 4

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.

MVC3 Display the edit view on the same page as the list of items

I have a list of transactions. When I click one of them, I'd like to be able to display the edit form (populated with the selected item's data) to the right of the list. Any ideas?
Below is the partial view that displays the list of transactions.
#model IEnumerable<BudgetPlus.Models.Transaction>
#{
ViewBag.Title = "Index";
}
<p>
#Html.ActionLink("Create New", "Create", null, new { #class = "button-link" })
</p>
<table>
<thead>
<tr>
<th class="column-Date">Date</th>
<th class="column-Description">Description</th>
<th class="column-Category">Category</th>
<th class="column-Amount">Amount</th>
<th class="action-button"></th>
<th class="action-button"></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Date)
</td>
<td>
#Html.DisplayFor(modelItem => item.Description)
</td>
<td>
#Html.DisplayFor(modelItem => item.Category.DisplayName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Amount)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.Id })
</td>
<td>
#Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
</tr>
}
</tbody>
</table>
<div>
#Html.EditorForModel("Edit")
</div>
And this is what the edit method in my TransactionController looks like.
public ActionResult Edit(int id)
{
Transaction transaction = db.Transactions.Find(id);
ViewBag.CategoryId = new SelectList(db.Categories, "Id", "DisplayName", transaction.CategoryId);
return View(transaction);
}
Probably the easiest way to achieve this is to use Ajax.Actionlink helper.
In your for loop have this instead of Html.Actionlink:
#Ajax.ActionLink("Edit", "Edit", new { id=item.Id }, new AjaxOptions { UpdateTargetId = "editor", HttpMethod = "Get", InsertionMode = InsertionMode.Replace });
And on the right side of your table:
<div id="editor"></div>
Then have a partial view for editing the Transaction, which will be rendered into the div with id "editor". You are using EditorForModel helper so I assume you have a partial view for editing already.
In your actionmethod return the partial view instead:
return PartialView("YourPartialView", transaction);

Categories

Resources