現在、ado.netとmvc3を使用しているブログのチュートリアル(c#で)に取り組んでいます。私はかなり新しい開発をしていますので、少し余裕を持ってください!とにかく、私は読んで(検証がオフになっている)ユーザーの投稿を担当するデータコントローラの1つをデバッグして、ブログに追加するのに問題があります。コードは、GetPost関数(コメント付き)で分解されます。 助けがあれば幸いです。問題のデバッグasp.net mvc3ブログアプリケーション:シーケンスには要素がありません
`using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; Blog.Modelsを使用している 。 using System.Text;私は例外*/
private Post GetPost(int? id)
{
return id.HasValue ? model.Posts.Where(x => x.ID == id).First() : new Post() { ID = -1 };
}
public bool IsAdmin { get { return true; } }
}
}
` EDIT君たちは右のお金であるうん午後04時29分GMT '配列に要素が含まれていない' 取得する場所
namespace Blog.Controllers
{
public class PostsController : Controller
{
private BlogModel model = new BlogModel();
public ActionResult Index()
{
return View();
}
[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.DateTime = dateTime;
post.Body = body;
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.AddToPosts(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 Tag GetTag(string tagName)
{
return model.Tags.Where(x => x.Name == tagName).FirstOrDefault() ?? new Tag() { Name = tagName };
}
/*ベローです!それはうんざりした、おかげで行きました! は、私が何かを私は何とかそれを宣言しなければならないのか、私が行方不明です。このビット
post.Title = title;
post.DateTime = dateTime;
post.Body = body;
にNullreference例外を取得し、今メートルいただきまし奇妙なのですか?
これはちょうどタイプミスでした。再度ありがとう