好的,所以我有 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; }
}
**此时我的县索引控制器**
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:传入字典的模型项的类型1[OilNGasWeb.ModelData.Clients]', but this dictionary requires a model item of type 'OilNGasWeb.ModelData.Clients'. at System.Web.Mvc.ViewDataDictionary
为 System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary dictionary) 的 System.Web.Mvc.ViewDataDictionary.DbQuery 1.SetModel(Object value)类型.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_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() 在 System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c_DisplayClass25 。_22(IAsyncResult asyncResult);