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

问题描述:

我有一个asp.net应用程序,我想根据数据库中找到的元素返回一个视图。我用这个代码:asp.net - Sequence不包含匹配的元素

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,但它不应该是因为我的数据中有搜索到的元素基地,我希望它找到它。 请帮忙!

+0

你试过'FirstOrDefault'而不是:'事件用户= events.FirstOrDefault(U => u.Id == ID)'?你想要搜索哪些表格元素? –

它看起来并不像你所查询的数据库,请尝试更改您的详细行动

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

按@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.... 
}