如何在mvc4中从控制器传递一对多到视图

好的,所以这将是你们帮助我的变化后,我假设我在某处得到语法错误

视图

@model OilNGasWeb.ModelData.Clients @{ ViewBag.Title = "Index"; } 

County's for

@Html.ActionLink("Create New", "Create",new { id = Model.ClientID },null)

@foreach (var item in Model.Countys) { }
@Html.DisplayNameFor(model => model.County) @Html.DisplayNameFor(model => model.Note) @Html.DisplayNameFor(model => model.Comment)
@Html.DisplayFor(modelItem => item.County) @Html.DisplayFor(modelItem => item.Note) @Html.DisplayFor(modelItem => item.Comment) @Html.ActionLink("Edit", "Edit", new { id=item.CountyID }) @Html.ActionLink("Details", "Details", new { id=item.CountyID }) @Html.ActionLink("Delete", "Delete", new { id=item.CountyID })

模型客户

  [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 { 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 { get; set; } } 

Countys控制器

 public ActionResult Index(int id) { var cnty = from r in db.Clients where r.ClientID == id select r; if (cnty != null) { return View(cnty); // View returns an error here } return HttpNotFound(); 

View返回错误但我无法介入它…找出它是什么……想法?

从它的外观来看,视图所需的数据与传递的数据不同。 您当前正在向视图发送IEnumerable 。 但是,正如您所问,枚举为空时会发生什么? 视图在哪里可以获得所需的其他数据? (在这种情况下是ClientID 。)

视图实际需要的是Clients 。 因为它正在寻找一块Clients级数据,即ClientID 。 当然,这些数据也存在于Countys对象上,但这对数据本身的概念性质并不重要。 此案例中的视图显示有关Clients对象的信息。 特别:

 int ClientID IEnumerable Countys 

如果这两者中的后者不为空,那么前者可以直接从该数据中辨别出来。 它也可能从完全不同的和不相关的数据中辨别出来。 但重点是视图在概念上是在Clients级别上运行,而不是在IEnumerable级别上。

因此,您需要相应地更改视图,并将其传递给它所需的对象:

 public ActionResult Index(int id) { var client = (from r in db.Clients where r.ClientID == id select r).SingleOrDefault(); if (client != null) return View(client); return HttpNotFound(); } 

出于可扩展性原因,您应该创建不属于您的域模型的ViewModel,并将它们传递到几乎所有视图中。

查看型号:

 public class IndexViewModel { public int ClientID { get; set; } public IEnumerable Clients { get; set; } } 

视图(.cshtml):

 @model OilNGasWeb.Models.Home.IndexViewModel @{ ViewBag.Title = "Index"; } 

County's for

// send a ClientID with this action link @Html.ActionLink("Create New", "Create", new { clientid = Model.ClientID } )

//... etc

我还建议你实际拼出你的变量。 这一切都被编译下来,因此通常可以在短处理变量上编写可维护代码。

调节器

 public ActionResult Index(int id) { //Lambda (just for an example, there is nothing wrong with LINQ expressions) var client = db.Clients .FirstOrDefault(c => c.ClientID == id); if (client != null) { var model = new IndexViewModel(); model.ClientID = id; model.Clients = // some value I don't understand // My preference/opinion (programming religion) is to prefix with this // so others know if the method is *this* class, *base* class etc return this.View(model); } return HttpNotFound(); }