Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 调试asp.net mvc3博客应用程序时遇到问题:序列不包含任何元素_C#_Asp.net_Asp.net Mvc 3_Ado.net - Fatal编程技术网

C# 调试asp.net mvc3博客应用程序时遇到问题:序列不包含任何元素

C# 调试asp.net mvc3博客应用程序时遇到问题:序列不包含任何元素,c#,asp.net,asp.net-mvc-3,ado.net,C#,Asp.net,Asp.net Mvc 3,Ado.net,我目前正在使用ado.net和mvc3(c#)编写博客教程。我对开发工作还比较陌生,所以请让我放松一下!无论如何,我在调试一个负责读取(验证关闭)用户帖子并将其添加到博客的数据控制器时遇到了问题。代码在GetPost函数处崩溃(用注释标记)。 任何帮助都将不胜感激 `使用制度; 使用System.Collections.Generic; 使用System.Linq; 使用System.Web; 使用System.Web.Mvc; 使用Blog.Models; 使用系统文本 namespace B

我目前正在使用ado.net和mvc3(c#)编写博客教程。我对开发工作还比较陌生,所以请让我放松一下!无论如何,我在调试一个负责读取(验证关闭)用户帖子并将其添加到博客的数据控制器时遇到了问题。代码在GetPost函数处崩溃(用注释标记)。 任何帮助都将不胜感激

`使用制度; 使用System.Collections.Generic; 使用System.Linq; 使用System.Web; 使用System.Web.Mvc; 使用Blog.Models; 使用系统文本

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 };
        }
/*下面是我得到异常“序列不包含元素”的地方*/

        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; } }
    }
}
` 编辑格林威治标准时间16:29是的,你们的钱是对的!非常好,谢谢! 奇怪的是我现在在这一点上得到了Nullreference异常

            post.Title = title;
            post.DateTime = dateTime;
            post.Body = body;
我是否必须以某种方式申报它们,还是我遗漏了什么


别管上面的编辑,那只是一个打字错误。再次感谢

尝试使用
FirstOrDefault()
。如果LINQ表达式不产生任何结果,则使用方法
First()
Single()
很可能会得到预期的“Sequence contains no elements”


编辑:使用
FirstOrDefault()
时,
默认值
表示
null
。您需要检查
GetPost
是否返回
null

我的猜测是
模型。Posts
不包含任何元素,因此尝试执行
First()
将导致出现错误


如果在集合中找不到项,则改用
FirstOrDefault()
将返回类型的默认值。

您的问题很可能是
model.Posts.Where(x=>x.ID==ID)
查询的结果为空,这使得
First()
方法抛出错误,这是查询空结果集的第一个元素时的默认行为


尝试使用
FirstOrDefalut
而不是
First
,这将返回引发异常的
null
intead。当然,测试您的结果是否为空aftewards,这样您就不会尝试使用空对象了