繁体   English   中英

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

[英]how to pass one-to-many from controller to view in mvc4

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

视图

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

出于可扩展性原因,您应该创建不属于您的域模型的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();
}

从它的外观来看,视图所需的数据与传递的数据不同。 您当前正在向视图发送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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM