如何通过一对许多来自控制器,以查看在mvc4 [英] how to pass one-to-many from controller to view in mvc4

查看:112
本文介绍了如何通过一对许多来自控制器,以查看在mvc4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以这将是改变,你们帮我之后,即时通讯假设即时得到一个语法错误的地方。

Ok so this would be after changes that you guys helped me with , im assuming im getting a syntax error somewhere

查看

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

}

示范Countys

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

}

Countys控制器

Countys controller

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 is returning an error but i cannot step into it ... to find out what it is ... ideas?

推荐答案

从外观上来看,通过视图所需要的数据在不同的级别的传递什么。您目前正在发送的IEnumerable&LT; Countys&GT; 到视图。但是,正如你问,当枚举是空的,会发生什么?视图可以从哪里获得其他部分,它需要的数据? (在这种情况下,客户端ID

From the looks of it, the data needed by the view is on a different level than what's being passed. You're currently sending an IEnumerable<Countys> to the view. But, as you ask, what happens when the enumeration is empty? Where can the view get the other pieces of data that it needs? (In this case the ClientID.)

什么看法的真正的需要的是一个客户端。因为它找了一块客户端 -level数据,即客户端ID 的。当然,这些数据也存在于 Countys 的对象,但是这无关紧要数据本身的概念性。在这种情况下,该视图显示约客户端对象信息。具体做法是:

What the view actually needs is a Clients. Because it's looking for a piece of Clients-level data, namely the ClientID. Sure, that data also exists on Countys objects, but that's immaterial to the conceptual nature of the data itself. The view in this case is showing information about a Clients object. Specifically:

int ClientID
IEnumerable<Countys> Countys

如果这两个的后者不是空的,则前者的可以的可以直接从这些数据看出端倪。它也可能从完全不同的和无关的数据,以及看出端倪。但问题是,鉴于在概念作用于客户端的水平,而不是一个的IEnumerable&LT; Countys&GT; 的水平。

If the latter of those two isn't empty then the former can be discerned directly from that data. It might also be discerned from entirely different and unrelated data as well. But the point is that the view is conceptually acting on a Clients level, not on an IEnumerable<Countys> level.

所以,你会相应地更改视图,并通过它的对象,它需要:

So you'd change the view accordingly, and pass it the object that it needs:

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

这篇关于如何通过一对许多来自控制器,以查看在mvc4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆