object does not contain a definition error when accessing dataset within MVC view
I am having a small problem geting to some data within my MVC C# application.
In the controller i have the following to retrieve a dataset to use in the View.
public ActionResult Franchisee()
{
getNav();
var data = (
from f in DietCenterDB.Franchises
join fc in DietCenterDB.FranchiseContents on f.ID equals fc.FranchiseId
into ffc
from subffc in ffc.DefaultIfEmpty()
select new {
State = f.State,
City = f.City,
AccountCode = f.AccountCode,
CenterNumber = f.CenterNumber,
FranchiseContentId = (subffc == null ? 0 : subffc.FranchiseContentId) }
).ToList();
return View(data);
}
The View has the following code
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Content.master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<dynamic>>" %>
<asp:Content ID="Head" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<h1>Franchisee Administration - <%: Html.ActionLink("Add New", "FranchiseeEdit", new { id = 0 }) %></h1>
» <%: Html.ActionLink("Content Management Home", "Index") %>
<p></p>
<table cellpadding="0" cellspacing="0" width="600px">
<%
int counter = 0;
string rowClass;
foreach (var item in Model) {
if ((counter % 2) == 0)
{
rowClass = "r1";
}
else
{
rowClass = "r2";
}
string dataString = item.State + " " + item.City + " " + item.AccountCode + " " + item.CenterNumber;
%>
<tr class="row <%: rowClass %>">
<td>
<%: Html.ActionLink(dataString, "FranchiseeEdit", new { id = item.ID, City = item.City, State = item.State, Zip = item.Zip })%>
</td>
开发者_运维问答 <td align="right">
<%: Html.ActionLink("Edit Microsite Content", "MicrositeContentEdit", new { id = item.ID, City = item.City, State = item.State, Zip = item.Zip })%>
</td>
</tr>
<% counter++; %>
<% } %>
</table>
</asp:Content>
When I run the app i get the following error
'object' does not contain a definition for 'State'
When debugging, the debugger shows that the data is in fact in memory.
Any tips or clues are greatly appreciated!!!
I'd say it's because you have a dynamic ViewPage, not a strongly-typed one:
Inherits="System.Web.Mvc.ViewPage<IEnumerable<dynamic>>"
Not saying its the problem, but will make things tricky.
Why don't you project your query into a ViewModel, and bind to that:
var data = (
from f in DietCenterDB.Franchises
join fc in DietCenterDB.FranchiseContents on f.ID equals fc.FranchiseId
into ffc
from subffc in ffc.DefaultIfEmpty()
select new MyViewModel {
State = f.State,
City = f.City,
AccountCode = f.AccountCode,
CenterNumber = f.CenterNumber,
FranchiseContentId = (subffc == null ? 0 : subffc.FranchiseContentId) }
).ToList();
And then in your View:
Inherits="System.Web.Mvc.ViewPage<IEnumerable<MyViewModel>>"
EDIT
MyViewModel
is a simple class you need to create, which allows lightweight storage for your model:
public class MyViewModel
{
public int State {get;set;}
public int City {get;set;}
public int AccountCode {get;set;}
public int CenterNumber {get;set;}
public int FranchiseContentId {get;set;}
}
精彩评论