Reputation: 85
I'm trying to access a two models in one view, basically i have two models
User Model
namespace SLMDemo0.Models
{
using System;
using System.Collections.Generic;
public partial class User
{
public int UID { get; set; }
public string UserName { get; set; }
public int PID { get; set; }
public int LID { get; set; }
public string SK { get; set; }
public string Brand { get; set; }
public string CN { get; set; }
public string SN { get; set; }
public string AT { get; set; }
public string Ref { get; set; }
public Nullable<System.DateTime> DC { get; set; }
public virtual License License { get; set; }
public virtual License License1 { get; set; }
}
}
Product Model
namespace SLMDemo0.Models
{
using System;
using System.Collections.Generic;
public partial class Product
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Product()
{
this.Licenses = new HashSet<License>();
}
public int PID { get; set; }
public int VenID { get; set; }
public string PName { get; set; }
public virtual Vendor Vendor { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<License> Licenses { get; set; }
}
}
And I have Details Action in User Controller
public ActionResult Details(int S)
{
SLMEntitiesDB dbContext = new SLMEntitiesDB();
var VL = (from U in dbContext.Users
join P in dbContext.Products
on U.PID equals P.PID
where P.PID == U.PID
select new UP()
{
UserO = U,
ProductO = P
}).Where(U => U.UserO.LID == S).ToList();
return View(VL);
}
@model SLMDemo0.Models.UP
@{
ViewBag.Title = "Details";
}
<h2>License Details</h2>
<p>
@using (Html.BeginForm("Details", "Users", FormMethod.Get))
{
<b>Search By:</b>
@Html.RadioButton("searchBy", "username") <text>Username</text>
@Html.TextBox("Search") <input type="submit" value="Search" />
}
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.UserO.UserName)
</th>
<th>
@Html.DisplayNameFor(model => model.ProductO.PName)
</th>
<th>
@Html.DisplayNameFor(model => model.UserO.SK)
</th>
<th>
@Html.DisplayNameFor(model => model.UserO.Brand)
</th>
<th>
@Html.DisplayNameFor(model => model.UserO.CN)
</th>
<th>
@Html.DisplayNameFor(model => model.UserO.SN)
</th>
<th>
@Html.DisplayNameFor(model => model.UserO.AT)
</th>
<th>
@Html.DisplayNameFor(model => model.UserO.DC)
</th>
<th>
@Html.DisplayNameFor(model => model.UserO.Ref)
</th>
<th></th>
</tr>
@foreach (var item in Model.ProductO)// issue is here
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.UserName)
</td>
<td>
@Html.DisplayFor(modelItem => item.SK)
</td>
<td>
@Html.DisplayFor(modelItem => item.Brand)
</td>
<td>
@Html.DisplayFor(modelItem => item.CN)
</td>
<td>
@Html.DisplayFor(modelItem => item.SN)
</td>
<td>
@Html.DisplayFor(modelItem => item.AT)
</td>
<td>
@Html.DisplayFor(modelItem => item.Ref)
</td>
<td>
@Html.DisplayFor(modelItem => item.DC)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.UID }) |
@Html.ActionLink("Details", "Details", new { id = item.UID }) |
@Html.ActionLink("Delete", "DelUL", new { L = item.UID }, new {
onclick = "return confirm('Are sure you want to permanently
delete this license ?');" })
</td>
</tr>
}
Now in details view, foreach has issue saying foreach statement cannot operate on variable of type Product
because product does not contain a public instance definition for GetEnumerator'.
Upvotes: 0
Views: 179
Reputation: 85
Thanks guys , it works in the View as below
@model IEnumerable<SLMDemo0.Models.UP>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.UserO.UserName)
</th>
Upvotes: 0
Reputation: 7601
Your query should be like this:
public ActionResult Details(int S)
{
SLMEntitiesDB dbContext = new SLMEntitiesDB();
var VL = (from U in dbContext.Users
join P in dbContext.Products
on U.PID equals P.PID
where P.PID == U.PID
select new UP(){
UserO = U,
ProductO = P
}).Where(U => U.LID == S).ToList();
return View(VL);
}
and the model should be
public class UP
{
public User UserO { get; set; }
public Product ProductO { get; set; }
}
Upvotes: 1