asp.net - Sequence不包含匹配元素

时间:2017-07-19 02:09:09

标签: c# sql asp.net

我有一个asp.net应用程序,我想根据我的数据库中找到的元素返回一个视图。我使用这段代码:

    public static List<Event> events = new List<Event>();
    public static int count = 0;

    // GET: Events
    public ActionResult Index()
    {
        _db = new ApplicationDbContext();

        return View(_db.Events.ToList());
    }

    public ActionResult Details(int Id)
    {
        Event user = events.First(u => u.Id == Id);
        return View(user);
    }

    [HttpGet]
    public ActionResult Create()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Create(Event evnt)
    {
        if (evnt.Title == null || evnt.Title.Equals(""))
        {
            return HttpNotFound("Nu s-a gasit anplm");
        }
        _db = new ApplicationDbContext();

        count++;
        evnt.Id = count;
        _db.Events.Add(evnt);
        _db.SaveChanges();

        return RedirectToAction("Index");
    }

    [HttpGet]
    public ActionResult Update(int id)
    {
        Event user = events.First(u => u.Id == id);
        return View("Create", user);
    }

    [HttpPost]
    public ActionResult Update(Event user)
    {
        Event exisingUser = events.First(u => u.Id == user.Id);
        exisingUser.Details = user.Details;
        exisingUser.Title = user.Title;
        exisingUser.DateAndTime = user.DateAndTime;
        exisingUser.Location = user.Location;

        return RedirectToAction("Index");
    }

    [HttpGet]
    public ActionResult Delete(int id)
    {
        Event user = events.First(u => u.Id == id);
        events.Remove(user);
        return RedirectToAction("Index");
    }
}

这是SQL:

CREATE TABLE [dbo].[Events] (
[Id]          INT            IDENTITY (1, 1) NOT NULL,
[Title]       NVARCHAR (MAX) NOT NULL,
[Details]     NVARCHAR (MAX) NULL,
[Location]    NVARCHAR (MAX) NULL,
[DateAndTime] NVARCHAR (MAX) NULL,
[Image]       NVARCHAR (MAX) NULL,
CONSTRAINT [PK_dbo.Events] PRIMARY KEY CLUSTERED ([Id] ASC));

模特:

public class Event
{
    public int Id { get; set; }

    [DisplayName("Title")]
    public string Title { get; set; }

    [DisplayName("Details")]
    public string Details { get; set; }

    [DisplayName("Location")]
    public string Location { get; set; }

    [DisplayName("Date and Time")]
    public string DateAndTime { get; set; }

    public string Image { get; set; }

}

我收到此错误: Error

我知道First()返回null,但它不应该因为我在数据库中有搜索到的元素,我希望它能找到它。 请帮忙!

2 个答案:

答案 0 :(得分:0)

您查询数据库时看起来没有,请尝试将详细信息操作更改为

public ActionResult Details(int Id)
{
    _db = new ApplicationDbContext();
    Event user = _db.Events.First(u => u.Id == Id);
    return View(user);
}

答案 1 :(得分:0)

根据@TetsuyaYamamoto的评论。

您可以考虑在代码中使用FirstOrDefault()来确定列表中是否存在记录。如果记录不存在,它将返回一个对象或null。

当然,不要忘记你的DBContext

_db = new ApplicationDbContext(); //initialized dbcontext.
var user = _db.Events.First(u => u.Id == Id).FirstOrDefault();
if (user != null) {
  return View(user);
}
else{
  //do something to show no record found....
}