Reputation: 1339
Ok so i have 2 models
Model Clients
[Table("Clients")]
public class Clients
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int ClientID { get; set; }
public string Client { get; set; }
public string Address { get; set; }
public string State { get; set; }
public string City { get; set; }
public string County { get; set; }
public int Zip { get; set; }
public string Phone { get; set; }
public string LogoLocation { get; set; }
public string ContactName { get; set; }
public string ContactPhone { get; set; }
public string ContactEmail { get; set; }
public int Authorized { get; set; }
public string Note { get; set; }
public string Comment { get; set; }
public virtual ICollection<Countys> Countys { get; set; }
}
Model Countys
[Table("Countys")]
public class Countys
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int CountyID { get; set; }
public int ClientID { get; set; }
public string County { get; set; }
public string Note { get; set; }
public string Comment { get; set; }
public virtual ICollection<TownShips> Townships { get; set; }
}
** My Countys Index Controller at this time **
public ActionResult Index(int id)
{
var cnty = from r in db.Clients
where r.ClientID == id
select r;
if (cnty != null)
{
try
{
return View(cnty);
}
catch (Exception ex)
{
}
}
return HttpNotFound();
}
My view is a standard generated view :
@model OilNGasWeb.ModelData.Clients
@{
ViewBag.Title = "Index";
}
<h2>County's for </h2>
<p>
@Html.ActionLink("Create New", "Create",new { id = Model.ClientID },null)
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Countys.First().County)
</th>
<th>
@Html.DisplayNameFor(model => model.Countys.First().Note)
</th>
<th>
@Html.DisplayNameFor(model => model.Countys.First().Comment)
</th>
</tr>
</Table>
EXCEPTION :
System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery1[OilNGasWeb.ModelData.Clients]', but this dictionary requires a model item of type 'OilNGasWeb.ModelData.Clients'. at System.Web.Mvc.ViewDataDictionary
1.SetModel(Object value) at System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary dictionary) at System.Web.Mvc.WebViewPage1.SetViewData(ViewDataDictionary viewData) at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1a. <InvokeActionResultWithFilters>b__17() at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func
1 continuation) at System.Web.Mvc.ControllerActionInvoker. <>c_DisplayClass1a.<>c_DisplayClass1c.b_19() at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c_DisplayClass25.<>c_DisplayClass2a. b_20() at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c_DisplayClass25. b_22(IAsyncResult asyncResult);
Upvotes: 1
Views: 4956
Reputation: 219047
If your view needs a Clients
object (which includes an enumeration of Countys
objects), then you need to pass a Clients
object to your view:
public ActionResult Index(int id)
{
var client = (from r in Clients
where r.ClientID == id
select r).SingleOrDefault();
if (client != null)
{
return View(client);
}
return HttpNotFound();
}
And in the view you'd declare a Clients
model:
@model OilNGasWeb.ModelData.Clients
Then in the view you would have access to the information about the Clients
object as well as the (potentially empty) list of Countys
objects it contains. Essentially what's "wrong" with your code is that the view is logically in the context of a Clients
(it's really hard not to just say "Client", you might want to work on the class names), but all you're giving it is an enumeration of Countys
objects.
Note: For safer code, you'll probably want to initialize that enumeration to whatever its value should be, in case it isn't populated in the event that it's an empty list. You'd do this in the class' constructor. For example, if you want to initialize it to a List<Countys>
then the constructor might be something like this:
public Clients()
{
Countys = new List<Countys>();
}
That way the property is never null
, at worst it's an empty list which is a lot easier to manage in the view.
Upvotes: 3