わかりましたので、私は2つのモデルを持っています
モデル クライアント
[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; }
}
モデル郡
[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 **
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();
}
私のビューは標準の生成ビューです:
@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>
例外 :
System.InvalidOperationException: ディクショナリに渡されたモデル項目の型は System.Data.Entity.Infrastructure.DbQuery 1[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.WebViewPage 1.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 継続) System.Web.Mvc.ControllerActionInvoker で。<>c_ DisplayClass1a .<>c _DisplayClass1c.b_ System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters の 19() (ControllerContext controllerContext、IList`1 フィルター、ActionResult actionResult) System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c の_DisplayClass25.<>c_DisplayClass2a . b System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c_DisplayClass25 の _20() 。_22(IAsyncResult asyncResult);