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

时间:2013-06-24 15:31:36

标签: c# asp.net-mvc one-to-many icollection

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

查看

@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.County) 
    </th>

    <th>
        @Html.DisplayNameFor(model => model.Note) 
    </th>

    <th>
        @Html.DisplayNameFor(model => model.Comment) 
    </th>

</tr>

@foreach (var item in Model.Countys) {
<tr>

    <td>
        @Html.DisplayFor(modelItem => item.County)
    </td>

    <td>
        @Html.DisplayFor(modelItem => item.Note)
    </td>

    <td>
        @Html.DisplayFor(modelItem => item.Comment)
    </td>

    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.CountyID }) 
        @Html.ActionLink("Details", "Details", new { id=item.CountyID }) 
        @Html.ActionLink("Delete", "Delete", new { id=item.CountyID })
    </td>

</tr>
}

</table>

模型客户

 [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; }

}

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正在返回一个错误,但我无法介入它...找出它是什么......想法?

2 个答案:

答案 0 :(得分:2)

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

查看型号:

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

查看(.cshtml):

@model OilNGasWeb.Models.Home.IndexViewModel

@{
  ViewBag.Title = "Index";
}


<h2>County's for </h2> 

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

//... 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();
}

答案 1 :(得分:1)

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

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

int ClientID
IEnumerable<Countys> Countys

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

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

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();
}