Jef*_* C. 5 c# asp.net asp.net-mvc ienumerable asp.net-mvc-4
我无法找到解决问题的方法,我尝试了很多替代方案,但我无法解决.
我首先使用模型生成我的数据库,之后我使用Scaffolding生成视图(索引,创建,编辑,删除..).唯一的视图(索引)与模型使用IEnumerable.
索引视图是:
@model IEnumerable<CAD_CMDBv2.Models.Location>
@{
ViewBag.Title = "Location's Management";
}
<h2>All Locations</h2>
<p>
@Html.ActionLink("Create Location", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Location.site_name)
</th>
<th>
@Html.DisplayNameFor(model => model.Location.country_name)
</th>
<th>
@Html.DisplayNameFor(model => model.Location.region_name)
</th>
<th></th>
</tr>
@foreach(var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Location.site_name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Location.country_name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Location.region_name)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Location.location_id }) |
@Html.ActionLink("Details", "Details", new { id = item.Location.location_id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Location.location_id })
</td>
</tr>
}
</table>
Run Code Online (Sandbox Code Playgroud)
我想为数据集插入一个异步表单,这样就变成:
@model IEnumerable<CAD_CMDBv2.Models.RechercheLocationViewModel>
@{
ViewBag.Title = "Location's Management";
}
<h2>All Locations</h2>
<p>
@Html.ActionLink("Create Location", "Create")
</p>
@using (Html.BeginForm("Search", "Restaurant", FormMethod.Get))
{
@Html.TextBoxFor(r => r.Recherche)
<input type="submit" value="Rechercher" />
<p>Search Results </p>
if (Model.ListeLocations.Count == 0)
{
<p> No Results but you can create it !</p>
}
else
{
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Location.site_name)
</th>
<th>
@Html.DisplayNameFor(model => model.Location.country_name)
</th>
<th>
@Html.DisplayNameFor(model => model.Location.region_name)
</th>
<th></th>
</tr>
@foreach(var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Location.site_name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Location.country_name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Location.region_name)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Location.location_id }) |
@Html.ActionLink("Details", "Details", new { id = item.Location.location_id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Location.location_id })
</td>
</tr>
}
</table>
}
}
Run Code Online (Sandbox Code Playgroud)
我在添加View Model类时修改了模型,以允许IndexView通过接管参数Locations并使用Search参数作为View Model的模型:
//------------------------------------------------------------------------------
// <auto-generated>
// Ce code a été généré à partir d'un modèle.
//
// Des modifications manuelles apportées à ce fichier peuvent conduire à un comportement inattendu de votre application.
// Les modifications manuelles apportées à ce fichier sont remplacées si le code est régénéré.
// </auto-generated>
//------------------------------------------------------------------------------
namespace CAD_CMDBv2.Models
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public partial class Location
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Location()
{
this.User = new HashSet<User>();
this.host = new HashSet<Host>();
this.client_catia = new HashSet<Client_catia>();
this.client_smartam = new HashSet<Client_smarteam>();
}
public int location_id { get; set; }
[Display(Name = "Site's Name")]
public string site_name { get; set; }
[Display(Name = "Country's Name")]
public string country_name { get; set; }
[Display(Name = "Region's Name")]
public string region_name { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<User> User { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Host> host { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Client_catia> client_catia { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Client_smarteam> client_smartam { get; set; }
}
public class RechercheLocationViewModel : IEnumerable<Location> {
public string Recherche {get; set;}
public Location Location { get; set; }
public List<Location> ListeLocations;
public IEnumerator<Location> GetEnumerator()
{
return ListeLocations.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ListeLocations.GetEnumerator();
}
}
}
Run Code Online (Sandbox Code Playgroud)
目前的控制器
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using CAD_CMDBv2.Models;
namespace CAD_CMDBv2.Areas.Locations.Controllers
{
public class LocationsController : Controller
{
private ModeleDonneesCMDBContext db = new ModeleDonneesCMDBContext();
// GET: Locations/Locations
public ActionResult Index()
{
var liste = db.Locations.ToList();
var listeTriee = liste.OrderBy(t => t.site_name);
return View(listeTriee);
}
...
Run Code Online (Sandbox Code Playgroud)
但是在行索引视图中会产生两个关于IEnumerable的相同类型的错误:
@Html.TextBoxFor(r => r.Recherche)
Run Code Online (Sandbox Code Playgroud)
和
if (Model.ListeLocations.Count == 0)
Run Code Online (Sandbox Code Playgroud)
我收到了这个错误:
CS1061错误"的IEnumerable"不包含关于"ListeLocations"的定义和没有扩展方法"ListeLocations"接受类型"的IEnumerable"的第一个参数可以找到(是否缺少using指令或程序集引用?)
那是什么意思?我该如何解决这个问题?理解IEnumerable接口仍然有些困难.
这就是你的错误所在:
var listeTriee = liste.OrderBy(t => t.site_name);
return View(listeTriee);
Run Code Online (Sandbox Code Playgroud)
您不是将单个模型传递给您,而是传递了一个确实不具有该属性的View集合 ( ) 。IEnumerableListeLocations
您应该创建一个视图模型并将集合放入其中:
public class ListeTrieeViewModel
{
...
public IEnumerable<Locations> ListeLocations {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
然后您可以在控制器中传递该模型:
public ActionResult Index()
{
var liste = db.Locations.ToList();
var listeTriee = liste.OrderBy(t => t.site_name);
var viewModel = new ListeTrieeViewModel { ListeLocations = listeTriee; }
return View(viewModel);
}
Run Code Online (Sandbox Code Playgroud)
现在您在视图中的签入将起作用:
if (Model.ListeLocations.Count() == 0)
Run Code Online (Sandbox Code Playgroud)