Explicit loading in EF Core randomly loads .Include() - asp.net-core

I have .NET 5 project with Index page where is table containing all rows from table Contracts, each row has Contract_StatusID column where is saved FK from table Contract_Statuses - table Contracts has more columns with this logic
Thing is, that Explicit loading is loading fully only last row of the table, other row's columns are randomly null, even when collection is .Include(s => s.Contract_Status) from controller code.
All ID columns are set, and these IDs are existing in related tables, but not loaded into this table on Index page.
Detail page always correctly load all .Include() relation tables but not on Index page.
I've validated my models and they looks OK, and work within other pages of the site.
What I am missing? Already went through MSDN and EFCore tutorials for this, but it just don't work.
public async Task<IActionResult> Index()
{
var contract = await context.Contracts
.Include(c => c.Employee)
.ThenInclude(c => c.Gender)
.Include(i => i.Business_Unit)
.Include(i => i.Brand)
.Include(i => i.Contract_Status)
.Include(i => i.Statutory_Unit)
.Include(i => i.Management_Market).ToListAsync();
return View(contract);
}
Edit: Adding my Index.cshtml
#model IEnumerable<MyProject.Models.Contract>
<table class="table table-hover">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.Employee)
</th>
<th>
#Html.DisplayNameFor(model => model.Contract_Status.Contract_Status)
</th>
<th>
#Html.DisplayNameFor(model => model.Business_Unit.Business_Unit)
</th>
<th>
#Html.DisplayNameFor(model => model.Brand.Brand)
</th>
<th>
#Html.DisplayNameFor(model => model.Statutory_Unit.Statutory_Unit)
</th>
<th>
#Html.DisplayNameFor(model => model.Management_Market.Management_Market)
</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Employee.EmployeeID)
</td>
<td>
#Html.DisplayFor(modelItem => item.Contract_Status.Contract_Status)
</td>
<td>
#Html.DisplayFor(modelItem => item.Business_Unit.Business_Unit)
</td>
<td>
#Html.DisplayFor(modelItem => item.Brand.Brand)
</td>
<td>
#Html.DisplayFor(modelItem => item.Statutory_Unit.Statutory_Unit)
</td>
<td>
#Html.DisplayFor(modelItem => item.Management_Market.Management_Market)
</td>
<td>
<a asp-action="Edit" asp-route-id="#item.ContractID">Edit</a> |
<a asp-action="Details" asp-route-id="#item.ContractID">Details</a>
</td>
</tr>
}
</tbody>
</table>
And my Contract model with example of related table
public class Contract
{
[Key]
[Required]
public Guid ContractID { get; set; }
[Required]
public string EmployeeID { get; set; }
public Employee Employee { get; set; }
[Required]
public int Contract_StatusID { get; set; }
public CONF_Contract_Status Contract_Status { get; set; }
[Required]
public int Business_UnitID { get; set; }
public CONF_Business_Unit Business_Unit { get; set; }
[Required]
public int BrandID { get; set; }
public CONF_Brand Brand { get; set; }
[Required]
public int Statutory_UnitID { get; set; }
public CONF_Statutory_Unit Statutory_Unit { get; set; }
[Required]
public int Management_MarketID { get; set; }
public CONF_Management_Market Management_Market { get; set; }
}
public class CONF_Contract_Status
{
[Key]
public int CS_ID { get; set; }
[Required]
[Display(Name = "Status")]
public string Contract_Status { get; set; }
[Required]
public bool Enabled { get; set; }
public Contract Contract { get; set; }
}
DB screenshot of Contract table
and CONF_Contract_Status

I usually alway create view model for index view, since it is usually needed several string columns, but it can have a lot of columsn. YOu don't need to bring from the server the whole record if you only need one column
public class ContractVieModel
{
public Guid ContractID { get; set; }
public string EmployeeName { get; set; }
public string Contract_Status { get; set; }
public string Business_Unit { get; set; }
.... and so on
}
action, you don't to use Include explicetly
public async Task<IActionResult> Index()
{
var model = await context.Contracts
.Select(i=> new ContractViewModel
{
Id=i.Id,
EmployeeName=i.Employee.Name
Contract_Status =i.Contract_Status.Contract_Status,
Business_Unit = i.Business_Unit.Business_Unit,
... and so on
}).ToListAsync();
return View(model);
}
view
#model IEnumerable<ContractViewModel>
......
try it , and if it is still not working then you will have to use left outer joins to get a view model

Related

Error al usar Dos modelos en una vista- mvc core

I have a model for Musico and another for Commune,
I try to bring both of them this way ..
I tried to use ** Tuple ** among other things but I went back to the base which was this
mi error..
public class ComunaViewModel
{
public Comuna ComunaDTO { get; set; }
public Musico MusicoDTO { get; set; }
}
musico
public class Musico
{
[Key]
public int Id { get; set; }
public string Nombre { get; set; }
public string Estilos { get; set; }
public int IdComuna { get; set; }
public string Email { get; set; }
Comuna
public class Comuna
{
[Key]
public int Id { get; set; }
public string Nombre { get; set; }
--
public async Task<IActionResult> Index()
{
return View(await _context.Musico.ToListAsync());
}
view
<table class="table">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.MusicoDTO.Nombre)
</th>
<th>
#Html.DisplayNameFor(model => model.MusicoDTO.Estilos)
</th>
<th>
#Html.DisplayNameFor(model => model.ComunaDTO.Nombre)
----
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.MusicoDTO.Nombre)
</td>
<td>
#Html.DisplayFor(modelItem => item.MusicoDTO.Estilos)
</td>
<td>
#Html.DisplayFor(modelItem => item.ComunaDTO.Nombre)
You need to pass List<ComunaViewModel> rather than List<Musico> to your view.
You can try to convert List<Musico> to List<ComunaViewModel>.
public async Task<IActionResult> Index()
{
List<Musico> l = await _context.Musico.ToListAsync();
List<ComunaViewModel> l1 = new List<ComunaViewModel>();
foreach (var item in l)
{
l1.Add(new ComunaViewModel { MusicoDTO = item });
}
return View(l1);
}

Entity framework core: Retrieve data to view in Many to many relationship

I have issue with reading related data in my Entity framework core in ASP.NET Core 2.2.
I have two entities Team and Player and because I deal with many to many relationship (team can have many players and players can have multiple teams), I have junction entity PlayerAssignments.
In players Index view, I want to show all players and in information about them - first name, last name and the teams they are assigned to, but I don't know how and I am trying to solve this for about two days now.
public class Player
{
public int id { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public ICollection<PlayerAssignments> PlayerAssignments { get; set; }
}
public class Team
{
public int id { get; set; }
public string name { get; set; }
public string city { get; set; }
public ICollection<PlayerAssignments> PlayerAssignments { get; set; }
}
public class PlayerAssignment
{
public int PlayerID { get; set; }
public int TeamID { get; set; }
public Player Player { get; set; }
public Team Team { get; set; }
}
everything similarly in dbcontext.
In my index method of PlayerController.cs:
var player = await _context.Player
.Include(one => one.PlayerAssignment)
.ThenInclude(team => team.Team)
.ThenInclude(teamName => teamName.name)
.ToListAsync();
if(player == null)
{
return NotFound();
}
return View(player);
}
My index view looks like this:
<table class="table">
<thead>
<tr>
<th>
First name
</th>
<th>
Last name
</th>
<th>
Player's teams
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.first_name)
</td>
<td>
#Html.DisplayFor(modelItem => item.last_name)
</td>
<td>
foreach(var assignedTeam in item.PlayerAssignments)
{
#* I just cant figure out code here *#
}
</td>
</tr>
</tbody>
<table>
I need to show all players with their info and the teams they are assigned to. But no matter what I tried, I never managed to write out the teams the players are assigned to (they are filled in database).
First of all your second thenInclude is redundant because that variable already is being included in the Team Object so change to this:
var player = await _context.Player
.Include(one => one.PlayerAssignment)
.ThenInclude(team => team.Team)
.ToListAsync();
if(player == null)
{
return NotFound();
}
return View(player);
And now all you have to do is display that Team 'Name' variable in the foreach loop which contains all the player team assigments like so:
<td>
foreach(var assignedTeam in item.PlayerAssignments)
{
#assignedTeam.Team.name
}
</td>

Reference model class in controller, so muliple data can be in one view

I am trying to get data from multiple SQL Server stored procedure to be accessible in 1 view so I can then run comparisons and create graphs/grids etc.
I got each section to work on their own, and I am now trying to get them to work together.
I have change my controller and put the field types into their own classes, and then created a "Ring of Rings" class, and put them all in.
namespace APP.Models
{
public class SP_RESULTS
{
public type Type { get; set; }
public status Status { get; set; }
public condition Condition { get; set; }
public rooms Rooms { get; set; }
}
public class type
{
[Key]
public decimal type_id { get; set; }
public string type_code { get; set; }
public string type_name { get; set; }
}
public class status
{
//status
public decimal status_id { get; set; }
public string status_code { get; set; }
public string status_name { get; set; }
public string rentable { get; set; }
}
public class condition
{
//condition
public decimal condition_id { get; set; }
public string condition_code { get; set; }
public string condition_name { get; set; }
public string rentable { get; set; }
public int colour_code { get; set; }
public int service_order { get; set; }
}
public class rooms
{
//rooms
public decimal room_id { get; set; }
public string room_no { get; set; }
public decimal type_id { get; set; }
public int floor { get; set; }
public decimal status_id { get; set; }
public decimal condition_id { get; set; }
}
}
I then amended each section of my controller that was running SQL Server stored procedures, to use the correct class name instead of the "ring of rings" name, IE:
var outputmodel4 = new List<rooms>();
var command4 = db.Database.Connection.CreateCommand();
command4.CommandText = "SELECT rooms.room_id , rooms.room_no , rooms.type_id , rooms.floor_no , rooms.status_id , rooms.condition_id FROM rooms";
using (var SPOutput4 = command4.ExecuteReader())
{
foreach (var row in SPOutput4)
{
outputmodel4.Add(new rooms()
{
room_id = (decimal)SPOutput4["room_id"],
room_no = (string)SPOutput4["room_no"],
type_id = (decimal)SPOutput4["type_id"],
floor_no = (int)SPOutput4["floor_no"],
status_id = (decimal)SPOutput4["status_id"],
condition_id = (decimal)SPOutput4["condition_id"],
});
}
db.Database.Connection.Close();
return View(outputmodel4);
}
...etc for other SQL stored procedures
..and the same with my view
#model IEnumerable<app.Models.SP_RESULTS >
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.Rooms.room_id)
</th>
<th>
#Html.DisplayNameFor(model => model.Rooms.room_no)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Rooms.room_id)
</td>
<td>
#Html.DisplayFor(modelItem => item.Rooms.room_no)
</td>
</tr>
}
</table>
…etc for the other models
Everything looks fine in VBS (no red swiggles), but when I access the view in the browser, I get an error:
Server Error in '/' Application.
The model item passed into the dictionary is of type 'System.Collections.Generic.List1[app.Models.rooms]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[app.Models.SP_RESULTS]'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Collections.Generic.List1[app.Models.rooms]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[app.Models.SP_RESULTS]'.
If I go back to my "rings of rings" model, and change it to this :
public class SP_RESULTS
{
public IEnumerable<type> Type { get; set; }
public IEnumerable<status> Status { get; set; }
public IEnumerable<condition> Condition { get; set; }
public IEnumerable<rooms> Rooms { get; set; }
}
and change the following lines in in my controller:
var outputmodel4 = new List<rooms>();
outputmodel4.Add(new rooms()
to
var outputmodel4 = new List<SP_RESULTS>();
outputmodel4.Add(new SP_RESULTS()
VBS tells me that that
SP_RESULTS does not contain a definition for room_id
I've tried prefixing the definition with the class name, with no luck.
I've looked on SO, fourms.asp.net and google.. can cannot see a solution (there probably is, but I am unsure what solution I an looking for.... if that makes sense).
Would someone be able to tell me what I need to do to get all my model classes working in the same view ?
I apologise now, as I realize that this question has probably been asked several (million) times, but I cannot seem to fine out that jumps out says THIS is that way to do it.
For anyone else looking for an answer (and for future me), I got this work by putting the models like this :
namespace APP.Models
{
public class SP_RESULTS
{
public type Type { get; set; }
public status Status { get; set; }
public condition Condition { get; set; }
public rooms Rooms { get; set; }
public class type
{
[Key]
public decimal type_id { get; set; }
public string type_code { get; set; }
public string type_name { get; set; }
}
public class status
{
//status
public decimal status_id { get; set; }
public string status_code { get; set; }
public string status_name { get; set; }
public string rentable { get; set; }
}
public class condition
{
//condition
public decimal condition_id { get; set; }
public string condition_code { get; set; }
public string condition_name { get; set; }
public string rentable { get; set; }
public int colour_code { get; set; }
public int service_order { get; set; }
}
public class rooms
{
//rooms
public decimal room_id { get; set; }
public string room_no { get; set; }
public decimal type_id { get; set; }
public int floor { get; set; }
public decimal status_id { get; set; }
public decimal condition_id { get; set; }
}
}
}
Using VIEWDATA in the controller for each Store procedure:
var outputmodel3 = new List<SP_RESULTS.conditions>();
var command3 = db.Database.Connection.CreateCommand();
command3.CommandText = "dbo.pr_PROCEDUREONE";
command3.CommandType = System.Data.CommandType.StoredProcedure;
using (var SPOutput3 = command3.ExecuteReader())
{
foreach (var row in SPOutput3)
{
outputmodel3.Add(new SP_RESULTS.conditions()
{
condition_id = (decimal)SPOutput3["condition_id"],
condition_code = (string)SPOutput3["condition_code"],
condition_name = (string)SPOutput3["condition_name"],
});
}
ViewData["CONDITIONSOutput"] = outputmodel3;
}
var outputmodel4 = new List<SP_RESULTS.rooms>();
var command4 = db.Database.Connection.CreateCommand();
command4.CommandText = "SELECT rooms.room_id , etc FROM rooms";
using (var SPOutput4 = command4.ExecuteReader())
{
foreach (var row in SPOutput4)
{
outputmodel4.Add(new SP_RESULTS.rooms()
{
room_id = (decimal)SPOutput4["room_id"],
room_no = (string)SPOutput4["room_no"],
});
}
ViewData["ROOMSOutput"] = outputmodel4;
db.Database.Connection.Close();
return View();
}
..and then changing my view to read :-
table class="table">
<tr>
<th>
Condition ID
</th>
<th>
Condition code
</th>
<th>
Condition name
</th>
<th>
is it rentable?
</th>
<th>
Condition color code
</th>
<th>
Condition service order
</th>
<th></th>
</tr>
#foreach (var item in ViewData["CONDITIONSOutput"] as IEnumerable<Happ.Models.SP_RESULTS.conditions>)
{
<tr>
<td>
#item.condition_id
</td>
<td>
#item.condition_code
</td>
<td>
#item.condition_name
</td>
<td>
#item.rentable
</td>
<td>
#item.colour_code
</td>
<td>
#item.service_order
</td>
</tr>
}
</table>
<table class="table">
<tr>
<th>
Room ID
</th>
<th>
Room No
</th>
<th>
Rooms type_id
</th>
<th></th>
</tr>
#foreach (var item in ViewData["ROOMSOutput"] as IEnumerable<APP.Models.SP_RESULTS.rooms>)
{
<tr>
<td>
#item.room_id
</td>
<td>
#item.room_no
</td>
</tr>
}
</table>

The specified type member 'Organizations' is not supported in LINQ to Entities

So I took over this project from school that utilizes MVC and I'm learning as I go. One thing the school wanted me to do was to add a new table to the database called Organization. They wanted the Organization to have a foreign key in the User table(AspNetUser) linking it to the Organization table with a primary key, Org_ID, and a organization name, Org_Type. They want every user to be a part of a specific organization. I've added that table to my database successfully. Now there is a table for the data entry that shows all the inputted data for the scholarshipCredits table. This table can currently be filtered by searching by email, but they want it to be capable of searching by last name or organization as well. I'm currently trying to get the organization to display in the table. If I change the display information in the View to the foreign key within AspNetUser it'll display correctly in the table but I want the Org_Type to show up. I joined the Organizations table to the linq statement "scholarshipCredits" in Controllers and I thought this would fix the issue but it has not. When I try to sort the column "organizations" it gives me this error: The specified type member 'Organizations' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported. Any help would be greatly appreciated.
View:
#model IEnumerable<L2E_MVC.Models.ScholarshipCredit>
#{
ViewBag.Title = "Data Entry";
Layout = "~/Views/Shared/_Layout - DataEntry.cshtml";
}
<h2>#ViewBag.Title</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
#using (Html.BeginForm())
{
<p>Search by: </p>
<p> #Html.RadioButton("searchType", "Email", true) Email </p>
<p> #Html.RadioButton("searchType", "Organization") Organization</p>
<p> #Html.RadioButton("searchType", "name") Last Name</p>
<p>
#Html.TextBox("SearchString")
<input type="submit" value="Search" />
</p>
}
<table class="table">
<tr>
<th>
#Html.ActionLink("Date", "Index", new { sortOrder = ViewBag.DateSortParm })
</th>
<th>
#Html.ActionLink("Is Spent", "Index", new { sortOrder = ViewBag.IsSpentSortParm })
</th>
<th>
#Html.ActionLink("Email", "Index", new { sortOrder = ViewBag.EmailSortParm })
</th>
<th>
#Html.ActionLink("Organization", "Index", new { sortOrder = ViewBag.OrgSortParm })
</th>
<th>
#Html.ActionLink("Activity", "Index", new { sortOrder = ViewBag.ActivitySortParm })
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Date)
</td>
<td>
#Html.DisplayFor(modelItem => item.IsSpent)
</td>
<td>
#Html.DisplayFor(modelItem => item.AspNetUser.Email)
</td>
<td>
#Html.DisplayFor(modelItem => item.Organizations.Org_Type)
</td>
<td>
#Html.DisplayFor(modelItem => item.StemActivity.Activity)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { pk1 = item.StemId, pk2 = item.UserId }) |
#Html.ActionLink("Details", "Details", new { pk1 = item.StemId, pk2 = item.UserId }) |
#Html.ActionLink("Delete", "Delete", new { pk1 = item.StemId, pk2 = item.UserId })
</td>
</tr>
}
</table>
Controller:
private Entities db = new Entities();
public ActionResult Index(string sortOrder, string searchString)
{
ViewBag.DateSortParm = sortOrder == "Date" ? "Date_desc" : "Date";
ViewBag.IsSpentSortParm = sortOrder == "IsSpent" ? "IsSpent_desc" : "IsSpent";
ViewBag.EmailSortParm = sortOrder == "Email" ? "Email_desc" : "Email";
ViewBag.ActivitySortParm = sortOrder == "Activity" ? "Activity_desc" : "Activity";
ViewBag.OrgSortParm = sortOrder == "Organization" ? "Organization_desc" : "Organization";
var scholarshipCredits = db.ScholarshipCredits.Include(s => s.AspNetUser).Include(s => s.StemActivity).Join(db.Organizations, s => s.AspNetUser.Org_ID, o => o.Org_ID, (s, o) => s);
if (!String.IsNullOrEmpty(searchString))
{
scholarshipCredits = scholarshipCredits.Where(s => s.AspNetUser.Email.Contains(searchString));
}
switch (sortOrder)
{
case "Date":
scholarshipCredits = scholarshipCredits.OrderBy(s => s.Date);
break;
case "Date_desc":
scholarshipCredits = scholarshipCredits.OrderByDescending(s => s.Date);
break;
case "IsSpent":
scholarshipCredits = scholarshipCredits.OrderBy(s => s.IsSpent);
break;
case "IsSpent_desc":
scholarshipCredits = scholarshipCredits.OrderByDescending(s => s.IsSpent);
break;
case "Email":
scholarshipCredits = scholarshipCredits.OrderBy(s => s.AspNetUser.Email);
break;
case "Email_desc":
scholarshipCredits = scholarshipCredits.OrderByDescending(s => s.AspNetUser.Email);
break;
case "Organization":
scholarshipCredits = scholarshipCredits.OrderBy(s => s.Organizations.Org_Type);
break;
case "Organization_desc":
scholarshipCredits = scholarshipCredits.OrderByDescending(s => s.Organizations.Org_Type);
break;
case "Activity":
scholarshipCredits = scholarshipCredits.OrderBy(s => s.StemActivity.Activity);
break;
case "Activity_desc":
scholarshipCredits = scholarshipCredits.OrderByDescending(s => s.StemActivity.Activity);
break;
default:
scholarshipCredits = scholarshipCredits.OrderBy(s => s.AspNetUser.Email);
break;
}
return View(scholarshipCredits.ToList());
}
Model:
public partial class ScholarshipCredit
{
public int StemId { get; set; }
public string UserId { get; set; }
public string Date { get; set; }
public Nullable<bool> IsSpent { get; set; }
public static double totalHours { get; set; }
public static double totalCredits { get; set; }
public static double spentCredits { get; set; }
public static double remainingCredits { get; set; }
public string userEmailSearch { get; set; }
public virtual AspNetUser AspNetUser { get; set; }
public virtual StemActivity StemActivity { get; set; }
public virtual Organizations Organizations { get; set; }
}
Entities:
public partial class Entities : DbContext
{
public Entities()
: base("name=Entities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<AspNetRole> AspNetRoles { get; set; }
public virtual DbSet<AspNetUserClaim> AspNetUserClaims { get; set; }
public virtual DbSet<AspNetUserLogin> AspNetUserLogins { get; set; }
public virtual DbSet<AspNetUser> AspNetUsers { get; set; }
public virtual DbSet<ScholarshipCredit> ScholarshipCredits { get; set; }
public virtual DbSet<StemActivity> StemActivities { get; set; }
public virtual DbSet<C__MigrationHistory> C__MigrationHistory { get; set; }
public virtual DbSet<Organizations> Organizations { get; set; }
}

MVC4 ViewModel to View from database - Example

Keep banging my head over seamlessly simple problem. Can somebody write me complete code for:
2 models:
public class State
{
public int StateID { get; set; }
public string Name { get; set; }
}
public class City
{
public int CityID { get; set; }
public string Name { get; set; }
public int StateID { get; set; }
[ForeignKey("StateID")]
public State State { get; set; }
public IEnumerable<SelectListItem> StateList { get; set; }
}
Problem is the rest. How to write ViewModel, Index ActionResult and View? Am using EF within built in testing environment.
I simply run out of combinations to try :(
#
And here is my final solution that works:
ViewModel
public class CityIndexViewModel
{
public int CityID { get; set; }
public string CityName { get; set; }
public int StateId { get; set; }
public string StateName { get; set; }
}
Controller
public ActionResult Index()
{
var model = new List<CityIndexViewModel>();
var dbCity = db.City.Include("State");
foreach (City c in dbCity)
{
var cityIndex = new CityIndexViewModel()
{
CityID = c.CityID,
CityName = c.Name,
StateId = c.State.StateID,
StateName = c.State.Name
};
model.Add(cityIndex);
}
return View(model);
}
View
#model IEnumerable<app.ViewModel.CityIndexViewModel>
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
Citys
#*#Html.DisplayNameFor(model => model.Citys)*#
</th>
<th>
States
#*#Html.DisplayNameFor(model => model.States)*#
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.CityName)
</td>
<td>
#Html.DisplayFor(modelItem => item.StateName)
</td>
<td>
#* #Html.ActionLink("Edit", "Edit", new { id=item.CityID }) |
#Html.ActionLink("Details", "Details", new { id=item.CityID }) |
#Html.ActionLink("Delete", "Delete", new { id=item.CityID })*#
</td>
</tr>
}
</table>