我无法为我的问题找到解决方案,我已经尝试了很多替代方案,但我无法解决它 .
我首先用模型生成我的数据库,之后我使用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>
我想为数据集插入一个异步表单,这样就变成:
@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>
}
}
我在添加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();
}
}
}
目前的控制器
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);
}
...
但是这会在索引视图中生成两个关于IEnumerable的相同类型的错误:
@Html.TextBoxFor(r => r.Recherche)
和
if (Model.ListeLocations.Count == 0)
我收到了这个错误:
CS1061错误'IEnumerable'不包含'ListeLocations'的定义,并且没有扩展方法'ListeLocations'接受类型'IEnumerable'的第一个参数可以找到(你是否缺少using指令或汇编引用?)
那是什么意思?我该如何解决这个问题?理解IEnumerable接口仍然有些困难 .
2 回答
您的
Location
类包含属性Recherche
和ListeLocation
,但该类的IEnumerable没有这些属性 .您正在使用该类的IEnumerable作为该类的实例,这是无法工作的 .
您应该考虑您的模型需要什么,因为在视图的一部分中,您使用
Model
就像它是Location
一样,而在另一部分(@foreach(var item in Model) {
)中,您将它用作IEnumerable当您使用
IEnumerable
界面作为模型时,您告诉View您有某种列表,集合或者您可以作为模型的东西 . 可以说,Location
对象的列表,而不是一个 .编辑以回应您的评论:将
@model IEnumerable<CAD_CMDBv2.Models.RechercheLocationViewModel>
更改为CAD_CMDBv2.Models.RechercheLocationViewModel
然后你需要你更改控制器动作:
代替 :
使用:
但那需要你所有位置的工作,那就是单一的
Location
,因为我不会_984490_返回搜索结果 . 你的表单现在只是在你的控制器上发布一些回复Search
动作,我不知道那个动作是做什么的 .我只能建议你学习一下如何在ASP.NET MVC中用AJAX创建搜索表单
这是你的错误:
您没有将单个模型传递给
View
,而是传递了一个确实没有ListeLocations
属性的集合(IEnumerable
) .您应该创建一个viewmodel并将集合放在那里:
然后你可以在你的控制器中传递该模型:
现在,您在视图中的检查将起作用: