1

好的,所以我有 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, Func1 续)在 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);

4

1 回答 1

3

如果您的视图需要一个Clients对象(包括对象的枚举Countys),那么您需要将一个Clients对象传递给您的视图:

public ActionResult Index(int id)
{
    var client = (from r in Clients
                 where r.ClientID == id
                 select r).SingleOrDefault();

    if (client != null)
    {
        return View(client);
    }
    return HttpNotFound();

 }

在视图中,您将声明一个Clients模型:

@model OilNGasWeb.ModelData.Clients

然后在视图中,您可以访问有关Clients对象的信息以及Countys它包含的(可能为空的)对象列表。本质上,您的代码的“错误”在于视图在逻辑上处于 a 的上下文中Clients真的很难不只是说“客户端”,您可能想要处理类名),但您所提供的只是对象的枚举Countys

注意:为了更安全的代码,您可能希望将该枚举初始化为其应为的任何值,以防万一它是一个空列表时未填充它。您可以在类的构造函数中执行此操作。例如,如果你想将它初始化为 aList<Countys>那么构造函数可能是这样的:

public Clients()
{
    Countys = new List<Countys>();
}

这样属性永远不会null,最坏的情况是它是一个空列表,在视图中更容易管理。

于 2013-06-28T18:02:16.477 回答