C# foreach语句错误GetEnumerator
我试图建立一个博客的基础上从VS2010教程,并使用VS2013我得到错误。任何帮助都将不胜感激。我的Foreach语句将不会执行。。我收到一个错误,说明Foreach语句无法对“blog.Models.Tag”类型的变量进行操作,因为blog.Models.Tag不包含Get Enumerator的公共定义C# foreach语句错误GetEnumerator,c#,model-view-controller,foreach,C#,Model View Controller,Foreach,我试图建立一个博客的基础上从VS2010教程,并使用VS2013我得到错误。任何帮助都将不胜感激。我的Foreach语句将不会执行。。我收到一个错误,说明Foreach语句无法对“blog.Models.Tag”类型的变量进行操作,因为blog.Models.Tag不包含Get Enumerator的公共定义 using System; using System.Collections.Generic; using System.Linq; using System.Web; using Sys
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using blog.Models;
using System.Text;
namespace blog.Controllers
{
public class PostsController : Controller
{
private BlogModel model = new BlogModel();
private const int PostsPerPage = 4;
public ActionResult Index(int? id)
{
int pageNumber = id ?? 0;
IEnumerable<Post> posts =
(from post in model.Posts
where post.DateTime < DateTime.Now
orderby post.DateTime descending
select post).Skip(pageNumber * PostsPerPage).Take(PostsPerPage + 1);
ViewBag.IsPreviousLinkVisible = pageNumber > 0;
ViewBag.IsNextLinkVisible = posts.Count() > PostsPerPage;
ViewBag.PageNumber = pageNumber;
ViewBag.IsAdmin = IsAdmin;
return View(posts.Take(PostsPerPage));
}
public ActionResult Details(int id)
{
Post post = GetPost(id);
ViewBag.IsAdmin = IsAdmin;
return View(post);
}
[ValidateInput(false)]
public ActionResult Comment(int id, string name, string email, string body)
{
Post post = GetPost(id);
Comment comment = new Comment();
comment.Post = post;
comment.Time = DateTime.Now;
comment.Name = name;
comment.Email = email;
comment.Body = body;
model.Comments.Add(comment);
model.SaveChanges();
return RedirectToAction("Details", new { id = id });
}
public ActionResult Tags(string id)
{
Tag Tag = (dynamic)GetTag(id);
ViewBag.IsAdmin = IsAdmin;
return View("Index", Tag.Posts);
}
[ValidateInput(false)]
public ActionResult Update(int? id, string title, string body, DateTime dateTime, string tags)
{
if(!IsAdmin)
{
return RedirectToAction("Index");
}
Post post = GetPost(id);
post.Title = title;
post.Body = body;
post.DateTime = dateTime;
post.Tags.Clear();
tags = tags ?? string.Empty;
string[] tagNames = tags.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
foreach(string tagName in tagNames)
{
post.Tags.Add(GetTag(tagName));
}
if(!id.HasValue)
{
model.Posts.Add(post);
}
model.SaveChanges();
return RedirectToAction("Details", new { id = post.Id });
}
public ActionResult Edit(int? id)
{
Post post = GetPost(id);
StringBuilder tagList = new StringBuilder();
foreach(Tag tag in post.Tags)
{
tagList.AppendFormat("{0} ", tag.Name);
}
ViewBag.Tags = tagList.ToString();
return View(post);
}
private object GetTag(string tagName)
{
return model.Tags.Where(x => x.Name == tagName).FirstOrDefault() ?? new Tag() {Name = tagName};
}
private Post GetPost(int? id)
{
return id.HasValue ? model.Posts.Where(x => x.Id == id).First() : new Post() { Id = -1 };
}
//TODO: Fix this later
public bool IsAdmin { get { return true; } }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
使用blog.Models;
使用系统文本;
名称空间blog.Controllers
{
公共类PostsController:控制器
{
私有BlogModel模型=新BlogModel();
private const int PostsPerPage=4;
公共行动结果索引(int?id)
{
int pageNumber=id±0;
数不清的职位=
(来自model.Posts中的post)
其中post.DateTime0;
ViewBag.IsNextLinkVisible=posts.Count()>PostsPerPage;
ViewBag.PageNumber=页码;
ViewBag.IsAdmin=IsAdmin;
返回视图(posts.Take(PostsPerPage));
}
公共行动结果详细信息(int id)
{
Post Post=GetPost(id);
ViewBag.IsAdmin=IsAdmin;
返回视图(post);
}
[验证输入(错误)]
公共操作结果注释(int-id、字符串名称、字符串电子邮件、字符串正文)
{
Post Post=GetPost(id);
注释=新注释();
comment.Post=Post;
comment.Time=DateTime.Now;
comment.Name=名称;
comment.Email=电子邮件;
评论:Body=Body;
model.Comments.Add(comment);
model.SaveChanges();
返回重定向到操作(“详细信息”,新的{id=id});
}
公共ActionResult标记(字符串id)
{
标记Tag=(动态)GetTag(id);
ViewBag.IsAdmin=IsAdmin;
返回视图(“索引”,Tag.Posts);
}
[验证输入(错误)]
公共操作结果更新(int?id、字符串标题、字符串正文、日期时间、字符串标记)
{
如果(!IsAdmin)
{
返回操作(“索引”);
}
Post Post=GetPost(id);
职位头衔=头衔;
post.Body=Body;
post.DateTime=日期时间;
post.Tags.Clear();
tags=标记??string.Empty;
string[]tagNames=tags.Split(新字符[]{''},StringSplitOptions.RemoveEmptyEntries);
foreach(标记名中的字符串标记名)
{
post.Tags.Add(GetTag(标记名));
}
如果(!id.HasValue)
{
model.Posts.Add(post);
}
model.SaveChanges();
返回RedirectToAction(“Details”,new{id=post.id});
}
公共行动结果编辑(int?id)
{
Post Post=GetPost(id);
StringBuilder标记列表=新建StringBuilder();
foreach(post.Tags中的标签)
{
tagList.AppendFormat(“{0}”,tag.Name);
}
ViewBag.Tags=tagList.ToString();
返回视图(post);
}
私有对象GetTag(字符串标记名)
{
返回model.Tags.Where(x=>x.Name==标记名).FirstOrDefault()??new Tag(){Name=tagName};
}
私人邮政GetPost(int?id)
{
返回id.HasValue?model.Posts.Where(x=>x.id==id).First():new Post(){id=-1};
}
//TODO:请稍后修复此问题
公共bool IsAdmin{get{return true;}}
}
}
这是我的标签。cs
namespace blog.Models
{
using System;
using System.Collections.Generic;
public partial class Tag
{
public Tag()
{
this.Posts = new HashSet<Post>();
}
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Post> Posts { get; set; }
internal void Clear()
{
//throw new NotImplementedException();
}
internal void Add(object p)
{
//throw new NotImplementedException();
}
}
}
namespace blog.Models
{
使用制度;
使用System.Collections.Generic;
公共部分类标记
{
公共标签()
{
this.Posts=newhashset();
}
公共int Id{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection Posts{get;set;}
内部空隙清除()
{
//抛出新的NotImplementedException();
}
内部空白添加(对象p)
{
//抛出新的NotImplementedException();
}
}
}
这是我的帖子
namespace blog.Models
{
using System;
using System.Collections.Generic;
public partial class Post
{
public Post()
{
this.Comments = new HashSet<Comment>();
}
public int Id { get; set; }
public string Title { get; set; }
public System.DateTime DateTime { get; set; }
public string Body { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
public virtual Tag Tags { get; set; }
}
}
namespace blog.Models
{
使用制度;
使用System.Collections.Generic;
公共部分类职位
{
公职人员职位()
{
this.Comments=newhashset();
}
公共int Id{get;set;}
公共字符串标题{get;set;}
public System.DateTime DateTime{get;set;}
公共字符串体{get;set;}
公共虚拟ICollection注释{get;set;}
公共虚拟标记{get;set;}
}
}
Foreach只能在实现GetEnumerator的集合上执行,因为它使用此函数获取Foreach的“下一个”项。显然,此函数不存在。错误消息相当简单。你不明白吗?这里显示了两个foreach
;一个在字符串[]
向量上,所以应该可以;另一个在Post.Tags
中,但不显示Post
类或Tags
定义。是不是这第二个foreach
正在崩溃?如果是这样的话:Post.Tags
是如何定义的?你好,马克,是的,这是第二条语句。请原谅,我对这一点还不熟悉,我正努力尽我所能遵循教程。