Stacie
asked on
MVC two joing table with link querry order by
I have this controller that return a grid but I would like to make some modification so my search result are order by location.
I have a table Master List that contain LastName, FirstName, and others fields and has one foreign key that is LocationID that is related to a Table phoneList Location and its primary key is ID.
My current controller work and return a result set:
The model item passed into the dictionary is of type 'System.Collections.Generi c.List`1[P honeListTe st.Models. PhoneListL ocation]', but this dictionary requires a model item of type 'System.Collections.Generi c.IEnumera ble`1[Phon eListTest. Models.Mas terList]'.
I have a table Master List that contain LastName, FirstName, and others fields and has one foreign key that is LocationID that is related to a Table phoneList Location and its primary key is ID.
My current controller work and return a result set:
var masterLists = db.MasterLists.Include(m => m.PhoneListLocation);
But when I made some modification bellow I get an error: var masterLists = from r in db.MasterLists.Include(m => m.PhoneListLocation)
orderby r.PhoneListLocation.ID
select r.PhoneListLocation;
return View(masterLists.ToList());
The model item passed into the dictionary is of type 'System.Collections.Generi
ASKER
Well this controller work
var masterLists = db.MasterLists.Include(m => m.PhoneListLocation);
but I'm trying to modified it so I can have an orderby from the location in the phoneListLocation...
var masterLists = db.MasterLists.Include(m => m.PhoneListLocation);
but I'm trying to modified it so I can have an orderby from the location in the phoneListLocation...
What is the relationship between MasterLists and PhoneListLocation? Is it a One to One, One to Many?
I think your hangup may be here:
c.IEnumera ble`1[Phon eListTest. Models.Mas terList]'.
-saige-
var masterLists = (from r in db.MasterLists.Include(m => m.PhoneListLocation)
orderby r.PhoneListLocation.ID
select r.PhoneListLocation);
// Not this - return View(masterLists.ToList());
// Use this instead:
return View(masterLists);
masterLists should already be a 'System.Collections.Generi-saige-
ASKER
But I get this error when I run the code....
Server Error in '/' Application.
The model item passed into the dictionary is of type 'System.Data.Entity.Infras tructure.D bQuery`1[W ebApplicat ionCodeFir stPhoneLis t.Models.P honeListLo cation]', but this dictionary requires a model item of type 'System.Collections.Generi c.IEnumera ble`1[WebA pplication CodeFirstP honeList.M odels.Mast erList]'.
Server Error in '/' Application.
The model item passed into the dictionary is of type 'System.Data.Entity.Infras
ASKER
the relation is one to many
Is your View method doing any sort of conversion? What does the view method take as a parameter?
-saige-
-saige-
ASKER
No no conversion
@model IEnumerable<WebApplicationCodeFirstPhoneList.Models.MasterList>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.PhoneListLocation.Location)
</th>
<th>
@Html.DisplayNameFor(model => model.LastName)
</th>
<th>
@Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
@Html.DisplayNameFor(model => model.Department)
</th>
<th>
@Html.DisplayNameFor(model => model.ExtensionInternal)
</th>
<th>
@Html.DisplayNameFor(model => model.ExtensionDirectDial)
</th>
<th>
@Html.DisplayNameFor(model => model.NumberMobile)
</th>
<th>
@Html.DisplayNameFor(model => model.NumberHome)
</th>
<th>
@Html.DisplayNameFor(model => model.NumberTextMessage)
</th>
<th>
@Html.DisplayNameFor(model => model.NumberOther)
</th>
<th>
@Html.DisplayNameFor(model => model.Terminated)
</th>
<th>
@Html.DisplayNameFor(model => model.Shift)
</th>
<th>
@Html.DisplayNameFor(model => model.Notes)
</th>
<th>
@Html.DisplayNameFor(model => model.NameGrouping)
</th>
<th>
@Html.DisplayNameFor(model => model.LastUpdatedDT)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.PhoneListLocation.Location)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Department)
</td>
<td>
@Html.DisplayFor(modelItem => item.ExtensionInternal)
</td>
<td>
@Html.DisplayFor(modelItem => item.ExtensionDirectDial)
</td>
<td>
@Html.DisplayFor(modelItem => item.NumberMobile)
</td>
<td>
@Html.DisplayFor(modelItem => item.NumberHome)
</td>
<td>
@Html.DisplayFor(modelItem => item.NumberTextMessage)
</td>
<td>
@Html.DisplayFor(modelItem => item.NumberOther)
</td>
<td>
@Html.DisplayFor(modelItem => item.Terminated)
</td>
<td>
@Html.DisplayFor(modelItem => item.Shift)
</td>
<td>
@Html.DisplayFor(modelItem => item.Notes)
</td>
<td>
@Html.DisplayFor(modelItem => item.NameGrouping)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastUpdatedDT)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.RecordID }) |
@Html.ActionLink("Details", "Details", new { id=item.RecordID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.RecordID })
</td>
</tr>
}
</table>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The two queries are returning different data types. The first is returning a collection of MasterLists while the modified query that does not work returns collection of PhoneListLocation. While I do not work with Web applications I believe that the View is not expecting the PhoneListLocation data type.
What will be needed in the View?