Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在mvc4中从控制器向视图传递一对多_C#_Asp.net Mvc_One To Many_Icollection - Fatal编程技术网

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

C# 如何在mvc4中从控制器向视图传递一对多,c#,asp.net-mvc,one-to-many,icollection,C#,Asp.net Mvc,One To Many,Icollection,好的,这是在你们帮我做了一些修改之后,我假设我在某个地方遇到了语法错误 看法 视图返回一个错误,但我无法介入。。。要知道它是什么。。。想法?从外观上看,视图所需的数据与正在传递的数据处于不同的级别。您当前正在向视图发送一个IEnumerable。但是,正如您所问的,当枚举为空时会发生什么?视图在哪里可以获得它需要的其他数据?(在本例中,ClientID) 视图实际需要的是一个客户端。因为它正在寻找一段客户端级别的数据,即ClientID。当然,这些数据也存在于Countys对象上,但这对数据本身

好的,这是在你们帮我做了一些修改之后,我假设我在某个地方遇到了语法错误

看法


视图返回一个错误,但我无法介入。。。要知道它是什么。。。想法?

从外观上看,视图所需的数据与正在传递的数据处于不同的级别。您当前正在向视图发送一个
IEnumerable
。但是,正如您所问的,当枚举为空时会发生什么?视图在哪里可以获得它需要的其他数据?(在本例中,
ClientID

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

int ClientID
IEnumerable<Countys> Countys

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

视图模型:

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

好的,很好,如果你不介意我问的话,你会如何在视图中访问该县,因为我也尝试过这个,目前在我的视图的最顶端--@model IEnumerable--我曾经显示namefor(model=>model.country)但如果我使用modeldata.countys,我将如何访问传递的countys?@Pakk:为什么您的模型是
IEnumerable
?你不是只需要一个
客户机
对象吗?至于传递的
Countys
值。。。
Clients
对象上没有名为
County
的字段。
客户机
对象上的字段称为
Countys
,它包含整个对象的枚举,因此我不确定框架如何解释该属性的“显示名称”。在视图中,是否存在通过该属性进入枚举的循环?该循环中的枚举对象的类型为
Countys
,因此应该具有
County
属性。是的,我相信你是对的,1个客户机多个县有多个镇,多个。。。。。麻生太郎。所以我在传递客户,但在我看来,我不想让它成为一个Ienumerable我在这方面是新手,我可以理解为什么我没有在我的客户机中循环,因为我传递了我想要的ID,那么一个引用如何引用一个客户机(或者我只是去掉Ienumerable关键字)?@Pakk:由于视图是针对单个
客户机
对象的,因此其模型声明将是
@model OilNGasWeb.ModelData.Clients
(可能是类型的命名导致了混淆?一个“客户机”的单个实例)被称为
客户机
,这是非常不直观的。当你有一个名为
Countys
Countys
对象列表时,它变得更加不直观了。好吧,这很有意义,那么我想,我之所以称它们为客户机,是因为我将在某个时候为管理页面的客户机列表使用相同的模型。。。但是回到手头的问题上,一个简单的客户端(countys中的字段名称)应该可以为我获取正确的数据吗?您能解释一下“public Guid ClientID{get;set;}”我不明白它如何知道引用ClientID表及其ID…根据您的注释进行更新。顺便说一句,奇怪的是,linq或lambda没有工作,但它确实[Bind(Prefix=“ID”)]int-CID){var-clnt=db.Clients.Find(CID);if(clnt!=null){return-View(clnt);}return-HttpNotFound();我要说的是没有传递任何东西,使用Linq或Lambda的客户机模型中的exept ClientID必须找到这个绑定方法,它传递了我所有的模型数据,wierd想更新所有人吗
 [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)
{
return View(cnty); // View returns an error here
}
return HttpNotFound();
int ClientID
IEnumerable<Countys> 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();
}
public class IndexViewModel
{
  public int ClientID { get; set; }
  public IEnumerable<Clients> Clients { get; set; }
}
@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();
}