asp.net mvc 5 get related data from different tables - c#

I'm trying to get specific data from different tables based on some requirements, The program is supposed to take some columns of choosing from tables: Contatti, Contacts and Companies which are at different locations and bring them together.
Contact is connected to Companies through CompanyID
What i want is to display the company name basing on the CompanyID field in Contact table. The problem is that i iterate through a list to get the data in the view, and because of that i can't seem to get a company name based on the company ID attribute which is the Contact Foreign Key to the Companies table, what i get is all the companies names, because they're in the same list.
I'm sure there is an easy way to do this but this is a new world for me, thank you for any help.
Contact Model:
public class Contact
{
[Key]
public int ContactId { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[ForeignKey("Companies")]
public int CompanyId { get; set; }
public virtual ICollection<Companies> Companies { get; set; }
[Required]
//public virtual Contatti Contatti { get; set; }
public virtual ICollection<Contatti> Contatti { get; set; }
}
Companies model:
public class Companies
{
[Key]
public int CompanyId { get; set; }
public string CompanyName { get; set; }
public string CompanyAddress { get; set; }
public string CompanyCity { get; set; }
public string CompanyState { get; set; }
public string CompanyZip { get; set; }
public string CompanyArea { get; set; }
}
Controller:
public ActionResult Index(String Page)
{
ContactsUni2 CU = new ContactsUni2();
CU.Contattis = db2.Contatti.ToList();
CU.Contacts = db.Contacts.ToList();
CU.Companies = db.Companies.ToList();
List<ContactsUni2> contactlist = new List<ContactsUni2>();
contactlist.Add(CU);
return View(contactlist);
}
View:
#foreach (var item in Model)
{
for (int i = 0; i < item.Contacts.Count; i++)
{
<tr>
<td>
#item.Contacts[i].ContactId
</td>
<td>
#item.Contacts[i].Name
</td>
<td>
#item.Contacts[i].Address
</td>
<td>
#item.Contacts[i].CompanyId
</td>
#if (#item.Contacts[i].CompanyId == item.Companies[i].CompanyId)
{
<td>
#item.Companies[i].CompanyName
</td>
<td>
#item.Companies[i].CompanyCity
</td>
<td>
#item.Companies[i].CompanyArea
</td>
}
}
</tr>
<tr>
<td>
#item.Contattis[i].ContattoID
</td>
<td>
#item.Contattis[i].Nome
</td>
<td>
#item.Contattis[i].Citta
</td>
<td>
#item.Contattis[i].CodicePostale
</td>
<td>
#item.Contattis[i].Email
</td>
</tr>
}
}
</table>
</body>
</html>

You can try populating the company collection for each of your contacts and then fix the view to access its own member directly instead of looking for it in the Companies list.
public ActionResult Index(String Page)
{
ContactsUni2 CU = new ContactsUni2();
CU.Contattis = db2.Contatti.ToList();
CU.Contacts = db.Contacts.ToList();
CU.Companies = db.Companies.ToList();
foreach(var contact in CU.Contacts)
{
contact.Companies = CU.Companies
.Where(com => com.CompanyId == contact.CompanyId)
.ToList();
}
List<ContactsUni2> contactlist = new List<ContactsUni2>();
contactlist.Add(CU);
return View(contactlist);
}
In the view, you can replace:
#if (#item.Contacts[i].CompanyId == item.Companies[i].CompanyId)
{
<td>
#item.Companies[i].CompanyName
</td>
<td>
#item.Companies[i].CompanyCity
</td>
<td>
#item.Companies[i].CompanyArea
</td>
}
With something like this: (Notice that the if statement is removed)
<td>
#item.Contacts[i].Companies[0].CompanyName
</td>
<td>
#item.Contacts[i].Companies[0].CompanyCity
</td>
<td>
#item.Contacts[i].Companies[0].CompanyArea
</td>
EDIT: Since ICollection does not support indexes, the companies collection should be changed from
public virtual ICollection<Companies> Companies { get; set; }
to
public virtual IList<Companies> Companies { get; set; }
Or any other type of collection supporting indexes.

Related

Inventory, OrderItems and Order

The problem I'm facing now is I have multiple Orders and each Order contains multiple items. Each Order has to link to a Client and Each item has to link to a Inventory Item.
Here's my Order Class
public class Order
{
[Key]
public int Id { get; set; }
public int TotalItems { get; set; }
public DateTime DeliveryDate { get; set; }
public string OrderNumber { get; set; }
public int ClientId { get; set; }
[ForeignKey("ClientId")]
public string DeliveryAddress { get; set; }
public List<OrderItem> OrderItems { get; set; }
public Client Clients { get; set; }
}
OrderItem Class
public class OrderItem
{
public int Id { get; set; }
public int OrderId{ get; set; }
[ForeignKey("OrderId")]
public int InventoryInfoId { get; set; }
[ForeignKey("InventoryInfoId")]
[Required]
public string ItemCode { get; set; }
public int Quantity { get; set; }
public InventoryInfo InventoryInfo { get; set; }
public Order Order { get; set; }
}
Any idea of how I can link them?
I think I have solved the above issues
As soon as I process, the next problem pops out.
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(m => item.OrderNumber)
</td>
<td>
#Html.DisplayFor(m=> item.DeliveryAddress)
</td>
<td>
#Convert.ToDateTime(item.DeliveryDate).ToString("dd-MM-yyyy")
</td>
<td>
#Html.DisplayFor(m=>item.Client.ClientCode)
</td>
<td>
#Html.DisplayFor(modelItem => item.TotalItems)
</td>
<td>
<a asp-action="Edit" asp-route-id="#item.Id" class="btn btn-success">Edit</a>
<a asp-controller="OrderItem" asp-action="OrderDetail" asp-route-id="#item.Id" class="btn btn-success">Details</a>
<a asp-action="Delete" asp-route-id="#item.Id" class="btn btn-success">Delete</a>
</td>
</tr>
}
This is my Orders Index page, when the details button is been clicked, the page should redirect to OrderItems page. However it doesnot.
#model List<IOSystem.Models.OrderItem>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(m => item.ItemCode)
</td>
<td>
#Html.DisplayFor(m => item.Quantity)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
#Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
</tr>
}
And here is the error message.
InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List1[IOSystem.Models.Order]', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.List1[IOSystem.Models.OrderItem]'.
Forgot to add my controller
[HttpPost, ActionName("OrderDetail")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> OrderDetailPost(int? id)
{
if (id == null)
{
return NotFound();
}
var orderItems = await _context.Orders.Include(s=>s.OrderItems).FirstOrDefaultAsync(i => i.OrderItemId == id);
return View(orderItems);
}
Your InvalidOperationException is saying you're passing a List<Order> but the model in your Razor page is a List<OrderItem>
In your controller code:
[HttpPost, ActionName("OrderDetail")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> OrderDetailPost(int? id)
{
if (id == null)
{
return NotFound();
}
var orderItems = await _context.Orders.Include(s=>s.OrderItems).FirstOrDefaultAsync(i => i.OrderItemId == id);
return View(orderItems);
}
You're accessing _context.Orders, which is your Orders table. You're .Include-ing the OrderItems but you're returning your Orders.
Either pass back the OrderItems from var orderItems = ... or adjust your Razor page's Model to be a List<Order>.
If you want to select the OrderItems from your Order in the controller code, update your LINQ statement to:
var orderItems = (await _context.Order
.Include(s => s.OrderItems)
.FirstOrDefaultAsync(i => i.OrderItemId == id))
.Select(x => x.OrderItems); // <-- grab just the OrderItems

Referencing 1 column from 1 model to another

I have a requirement to show 1 column from different model on MVC. Im a first timer in mvc so im not really familiar with lambda convention...
So far I have this classes
article.cs
public class Article
{
[Key]
public int bl_id { get; set; }
[Column("bl_title")]
public string Title { get; set; }
[Column("bl_img")]
public string Image { get; set; }
[Column("bl_summ")]
public string Summary { get; set; }
[AllowHtml]
[Column("bl_content")]
public string Content { get; set; }
[Column("bl_author")]
public string Author { get; set; }
[DisplayFormat(ApplyFormatInEditMode =true, DataFormatString ="{0:MM/dd/yyyy}")]
[Column("bl_dtecrt")]
public DateTime DateCreated { get; set; }
}
the next class is the comments:
comments.cs
public class Comments
{
[Key]
public int ic_id { get; set; }
public int ic_blid { get; set; }
public string ic_comment { get; set; }
public DateTime ic_crtdte { get; set; }
public string ic_pcname { get; set; }
}
What I need is to have relate the Title Column from the Articles.cs models to the set of Comments... if it were on sql i could do a select query in this manner: select article.title, comments.ic_comment from articles, comments where articles.bl_id = comments.ic_blid
So far, the solution i tried is create a join in the controller then throw it to the view...
articlescontroller.cs
public ActionResult Comments()
{
var comm = from c in db.Comments
join a in db.Articles
on c.ic_blid equals a.bl_id
select c;
return View("Comments", comm.ToList());
}
EDIT: View
Comments.cshtml
#model IEnumerable<WebApplication3.Models.Comments>
#{
ViewBag.Title = "Comments";
Layout = "~/Views/Shared/_Layout.cshtml";
}
#Html.DisplayNameFor(model => model.ic_comment)
</th>
<th>
#Html.DisplayNameFor(model => model.ic_crtdte)
</th>
<th>
#Html.DisplayNameFor(model => model.ic_pcname)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
#Html.DisplayFor(ModelItem => item.Article.Title)
<tr>
<td>
#Html.DisplayFor(modelItem => item.ic_comment)
</td>
<td>
#Html.DisplayFor(modelItem => item.ic_crtdte)
</td>
<td>
#Html.DisplayFor(modelItem => item.ic_pcname)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.ic_id }) |
#Html.ActionLink("Details", "Details", new { id=item.ic_id }) |
#Html.ActionLink("Delete", "Delete", new { id=item.ic_id })
</td>
</tr>
}
</table>
but my view is not catering the columns from the articles.
So my question is, how can I incorporate 1 or more columns from my Articles Model to my Comments Models then show them in the view? The reason behind this module is to maintain the comments done in the articles posted.
I tried putting this in the comments model:
public virtual Article Article { get; set; }
but i dont know how to use.
I hope you can help me...
You will first have to correct your Article class.
Since a Article can have many comments you need a property describing the same
(Please note the code is not tested)
public class Article{
...
public List<Comment> Comments {get; set;}
...
}
Now the place where you are trying to retrieve the data, include comments as well. Something like this:
public ActionResult Comments()
{
var comm = db.Article.Include(x=>x.Comments).ToList();
// This will get All article and include comments if any for each article.
return View("Comments", comm.ToList());
}
In your view you would have to iterate Comment for individual Article
#{
ViewBag.Title = "Comments";
Layout = "~/Views/Shared/_Layout.cshtml";
}
#foreach (var item in Model) {
#Html.DisplayFor(ModelItem => item.Article.Title)
<tr>
<td>
#Html.DisplayFor(modelItem => item.Comments.ic_comment)
</td>
<td>
#Html.DisplayFor(modelItem => item.Comments.ic_crtdte)
</td>
<td>
#Html.DisplayFor(modelItem => item.Comments.ic_pcname)
</td>
<td
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.ic_id }) |
#Html.ActionLink("Details", "Details", new { id=item.ic_id }) |
#Html.ActionLink("Delete", "Delete", new { id=item.ic_id })
</td>
</tr>
}
This can be done in below 2 ways. Create a Class which contains Comments and Title. Construct the object from this select result and pass it to Model.
Or have a Navigation property for comments in Article class. And modify LINQ-SQL as below.
Artice class
public class Article
{
[Key]
public int bl_id { get; set; }
[Column("bl_title")]
public string Title { get; set; }
[Column("bl_img")]
public string Image { get; set; }
[Column("bl_summ")]
public string Summary { get; set; }
[AllowHtml]
[Column("bl_content")]
public string Content { get; set; }
[Column("bl_author")]
public string Author { get; set; }
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
[Column("bl_dtecrt")]
public DateTime DateCreated { get; set; }
public virtual ICollection<Comments> Comments { get; set; }
}
Query
var comments1 = from c in comments
join a in articles
on c.ic_blid equals a.bl_id
select a;
View as below
#model IEnumerable<WebApplication1.Models.Article>
#{
ViewBag.Title = "Comments";
}
<h2>Comments</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>Title</th>
<th>ic_comment</th>
<th>ic_blid</th>
<th>ic_crtdte</th>
<th>ic_pcname</th>
</tr>
#foreach (var item1 in Model)
{
foreach (var item in item1.Comments)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item1.Title)
</td>
<td>
#Html.DisplayFor(modelItem => item.ic_comment)
</td>
<td>
#Html.DisplayFor(modelItem => item.ic_blid)
</td>
<td>
#Html.DisplayFor(modelItem => item.ic_crtdte)
</td>
<td>
#Html.DisplayFor(modelItem => item.ic_pcname)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.ic_id }) |
#Html.ActionLink("Details", "Details", new { id=item.ic_id }) |
#Html.ActionLink("Delete", "Delete", new { id=item.ic_id })
</td>
</tr>
}
}
</table>
Need to declare the Model for the View as dynamic. Please see
https://www.aspsnippets.com/Articles/Display-data-in-Single-View-from-Multiple-Tables-in-ASPNet-MVC.aspx
if it helps.
Also modify as below
var comm = from c in db.Comments
join a in db.Articles
on c.ic_blid equals a.bl_id
select new {c,a.title};
The answer of #Jja is the accepted answer but since i had a trouble with navigation property...
As per this stackoverflow link: The Name value should be a valid navigation property name.
My problem when using ForeignKey, the virtual table you're connecting to must have the same name on the [ForeignKey("")] tag. Before, my Comments class was defined as this:
[Table("Intranet_Comments")]
public class Intranet_Comments
{
[Key]
public int ic_id { get; set; }
[ForeignKey("Intranet_Article")]
public int ic_blid { get; set; }
public string ic_comment { get; set; }
public DateTime ic_crtdte { get; set; }
public string ic_pcname { get; set; }
public virtual Article Article { get; set; }
}
And it throws navigation error... i re-wrote this class into:
[Table("Intranet_Comments")]
public class Intranet_Comments
{
[Key]
public int ic_id { get; set; }
[ForeignKey("Article")]
public int ic_blid { get; set; }
public string ic_comment { get; set; }
public DateTime ic_crtdte { get; set; }
public string ic_pcname { get; set; }
public virtual Article Article { get; set; }
}
The foreign key property name must be the same name as the virtual property set..
i hope this helps!

.NET Use A Related Table To Decide What Information to Show

I have a client model a identifier model and a link model. The identifier model contains websites available for posting, client model contains clients that need a specific amount of links per month. A link object in the link model contains the Identifier ID and the Client ID. I want another view displaying a table of all the Identifier ID's that haven't been used with a Client ID's in the Link model table if this makes sense?
Client Model:
namespace Linkofy.Models
{
public class Client
{
public int ID { get; set; }
[Required]
[Display(Name = "Client")]
public string clientN { get; set; }
[Url]
[Display(Name = "Website")]
public string homePage{ get; set; }
[EmailAddress]
[Display(Name = "Contact Email")]
public string clientEmail { get; set; }
[Display(Name = "Contact Name")]
public string contName { get; set; }
[Display(Name = "Monthly")]
public int monthlyQuota { get; set; }
[Display(Name = "TF")]
public int TrustFlow { get; set; }
[Display(Name = "CF")]
public int CitationFlow { get; set; }
public int RI { get; set; }
public int? MJTopicsID { get; set; }
public virtual MJTopics MJTopics { get; set; }
public int UserTableID { get; set; }
public virtual UserTable UserTable { get; set; }
public virtual ICollection<Link> Links { get; set; }
public virtual ICollection<Status> Statuss { get; set; }
}
}
Link Model:
namespace Linkofy.Models
{
public class Link
{
public int LinkID { get; set; }
[Required]
[Display(Name = "Linking Page")]
public string Obdomain { get; set; }
public int? ClientID { get; set; }
public virtual Client Client { get; set; }
[Required]
[Display(Name = "Outbound Link")]
public string Obpage { get; set; }
[Required]
[Display(Name = "Anchor Text")]
public string Anchor { get; set; }
[Required]
[Display(Name = "Date Built")]
public DateTime BuildDate { get; set; }
public int IdentifierID { get; set; }
public virtual Identifier Identifier { get; set; }
public int? UserTableID { get; set; }
public virtual UserTable UserTable { get; set; }
}
}
Domain Model:
public class Identifier
{
public enum Ltype
{
GuestPost, ExistingLink
}
public int ID { get; set; }
[Url]
[Required]
[Display(Name = "Domain")]
public string domain { get; set; }
[Required]
[Display(Name = "Contact Email")]
[EmailAddress]
public string contact { get; set; }
[Display(Name = "Contact Name")]
public string contactname { get; set; }
[Required]
[Display(Name = "Price")]
public int price { get; set; }
[Display(Name = "Type of Link")]
public Ltype? type { get; set; }
[Display(Name = "TF")]
public int TrustFlow { get; set; }
[Display(Name = "CF")]
public int CitationFlow { get; set; }
public int RI { get; set; }
public int? MJTopicsID { get; set; }
public virtual MJTopics MJTopics { get; set; }
public virtual UserTable UserTable { get; set; }
public int UserTableID { get; set; }
public virtual ICollection<Link> Links { get; set; }
}
}
Model View:
#model IEnumerable<Linkofy.Models.Identifier>
#{
ViewBag.Title = "Index";
}
#section Styles {
<link href="#Url.Content("~/Styles/Index.css")" rel="stylesheet" type="text/css" />
}
<h1>Domain List</h1>
<p class="options"> #Html.ActionLink("Add New", "Create") | #Html.ActionLink("Add Bulk", "CreateBulk") </p>
<div class="row">
<div class="col-md-12">
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.domain)
</th>
<th>
#Html.DisplayNameFor(model => model.contact)
</th>
<th>
#Html.DisplayNameFor(model => model.contactname)
</th>
<th>
#Html.DisplayNameFor(model => model.price)
</th>
<th>
#Html.DisplayNameFor(model => model.type)
</th>
<th>
#Html.DisplayNameFor(model => model.TrustFlow)
</th>
<th>
#Html.DisplayNameFor(model => model.CitationFlow)
</th>
<th>
#Html.DisplayNameFor(model => model.RI)
</th>
<th>
#Html.DisplayNameFor(model => model.MJTopics.topicalTF)
</th>
<th></th>
</tr>
#foreach (var item in Model.Where(Model.Links.Item => item.ClientID != ViewBag.ClientID)) {
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.domain)
</td>
<td>
#Html.DisplayFor(modelItem => item.contact)
</td>
<td>
#Html.DisplayFor(modelItem => item.contactname)
</td>
<td>
#Html.DisplayFor(modelItem => item.price)
</td>
<td>
#Html.DisplayFor(modelItem => item.type)
</td>
<td>
#Html.DisplayFor(modelItem => item.TrustFlow)
</td>
<td>
#Html.DisplayFor(modelItem => item.CitationFlow)
</td>
<td>
#Html.DisplayFor(modelItem => item.RI)
</td>
<td>
#Html.DisplayFor(modelItem => item.MJTopics.topicalTF)
</td>
<td>
#Html.ActionLink(" ", "Details", new { id=item.ID }, new {#class= "glyphicon glyphicon-home" })
#Html.ActionLink(" ", "Edit", new { id=item.ID }, new {#class= "glyphicon glyphicon-edit" })
#Html.ActionLink(" ", "Delete", new { id=item.ID }, new {#class= "glyphicon glyphicon-trash" })
</td>
</tr>
}
</table>
</div>
</div>
I achieved this using LINQ statements as mentioned by Zorkind in comments.

How to display data from multiple table in list view asp.net MVC 5 or 6 using viewModel

How can I achieve the same result using my view Model
Model 1:
public class Unit
{
public int Id { get; set; }
[Required]
public string UnitName { get; set; }
public virtual ICollection<Staff> Staffs { get; set; }
}
Model 2:
public class Staff
{
public int Id { get; set; }
public string FullName { get; set; }
public int UnitId { get; set; }
public virtual Unit Unit { get; set; }
}
My ViewModel:
public class StaffVM
{
public int Id { get; set; }
public string FullName { get; set; }
public int UnitId { get; set; }
public string UnitName { get; set; }
}
Controller Index function:
// GET: Staffs
public ActionResult Index()
{
var query = from c in db.MyStaff
join d in db.MyUnit on c.UnitId equals d.Id
select new StaffVM
{
Id = c.Id,
FullName = c.FullName,
UnitName = d.UnitName
};
ViewBag.query = query;
return View();
}
My View Index view:
#model IEnumerable<MVC5WAuth.ViewModels.StaffVM>
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.FullName)
</th>
<th>
Unit
</th>
<th></th>
</tr>
#foreach (var item in ViewBag.query) {
<tr>
<td>
#item.FullName
</td>
<td>
#item.UnitName
</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>

Print Table with List Variable From Entities in MVC

I've not worked MVC for a long time; I'm a fresh guy to this. So, I have these two entities:
// Entity Worker
[Display(Name = "ID")]
public Int32 Id { get; set; }
[Display(Name = "Número")]
public Int32 Numero { get; set; }
[Display(Name = "Nome")]
// Entity Lottery
public Int32 Id { get; set; }
[Display(Name = "Tipo")]
public String Tipo { get; set; }
[Display(Name = "Data")]
public DateTime Data { get; set; }
[Display(Name = "Observações")]
public List<Worker> FuncionariosSorteados { get; set; }
So, for each lottery entity, I will have a List of workers. I am passing the values to the View by the controller, like this:
public ActionResult Details(int id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Lottery lottery = service.FindLottery(id);
if (sorteio == null)
{
return HttpNotFound();
}
return View(lottery);
}
Where the service is my Repository for the connection to database (in this case he do a search by ID on database to get the right lottery.
Here my doubt begin, if I want to do a table for the lotteries, I can do it doing (using Lottery model)
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Tipo)
</td>
<td>
#Html.DisplayFor(modelItem => item.Data)
</td>
<td>
#Html.DisplayFor(modelItem => item.Observacoes)
</tr>
}
But how I can do the same for the public List<Worker> FuncionariosSorteados? I just want to print the public List<Worker> FuncionariosSorteados in a GridMvc table, but through the model I cant have access to it!
I believe you need to iterate through the list within the foreach loop you already created. Something like this:
#foreach (var item in Model)
{
<table>
<tr>
<td>
#Html.DisplayFor(modelItem => item.Tipo)
</td>
<td>
#Html.DisplayFor(modelItem => item.Data)
</td>
<td>
#Html.DisplayFor(modelItem => item.Observacoes)
</td>
</tr>
</table>
<table>
#foreach(var w in Model.FuncionariosSorteados )
{
<tr>
<td>#Html.DisplayFor(w => w.Id)</td>
<td>#Html.DisplayFor(w => w.Numero)</td>
</tr>
}
</table>
}
I'm confused because in your action, you're pulling a single lottery record and passing that to your view, but in your view, you're iterating through what appears to be an IEnumerable<Lottery>.
Since you'd have no issues accessing FuncionariosSorteados off of a Model of type Lottery, I'm assuming the view is actually using IEnumerable<Lottery>. For an enumerable, you have to iterate over the list and access the properties on the individual items. For example:
#model IEnumerable<Lottery>
#Model.FuncionariosSorteados <!-- fail -->
#foreach (var lottery in Model)
{
#lottery.FuncionariosSorteados <!-- success -->
}
For your second entity you could create it like this:
Entitie Lottery
public Lottery( )
{
FuncionariosSorteados = new List <Worker>();
}
public Int32 Id { get; set; }
[Display(Name = "Tipo")]
public String Tipo { get; set; }
[Display(Name = "Data")]
public DateTime Data { get; set; }
[Display(Name = "Observações")]
public virtual List<Worker> FuncionariosSorteados { get; set; }
And thenin your view you can use a foreach loup to iterate inside the lists

Categories

Resources