1

好的,这将是在你们帮助我进行更改之后,我假设我在某处遇到语法错误

看法

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

}

县控制器

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

4

2 回答 2

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();
}
于 2013-06-24T16:30:27.580 回答
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();
}
于 2013-06-24T15:41:01.537 回答