我正在尝试根据VS 2010的教程构建博客,并使用VS 2013我收到错误。任何帮助,将不胜感激。我的Foreach语句将不会执行..我收到一条错误,指出Foreach语句无法对类型为' blog.Models.Tag'的变量进行操作。因为blog.Models.Tag不包含Get Enumerator的公共定义...
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; } }
}
}
这是我的 Tag.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();
}
}
}
这是我的Posts.cs
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; }
}
}
答案 0 :(得分:-2)
Foreach只能在实现GetEnumerator的集合上完成,因为它使用此函数来获取&#34; next&#34;项目到foreach上。显然这个功能不存在。