简体   繁体   English

asp.net-序列不包含匹配元素

[英]asp.net - Sequence contains no matching element

I have an asp.net application in which I want to return a view based on the element found in my database. 我有一个asp.net应用程序,我想在其中基于数据库中找到的元素返回一个视图。 I use this code: 我使用以下代码:

    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");
    }
}

Here is the SQL: 这是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));

And the model: 和模型:

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; }

}

And I get this error: Error 我得到这个错误: 错误

I know that the First() returns null, but it shouldn't because I have the searched element in my database, I want it to find it. 我知道First()返回null,但是不应该这样,因为我的数据库中有被搜索的元素,我希望它能够找到它。 Please help! 请帮忙!

It doesn't look like you are querying the database, try changing your Details action to 看起来好像不是在查询数据库,请尝试将Details动作更改为

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

As per comment of @TetsuyaYamamoto. 根据@TetsuyaYamamoto的评论。

You could consider using FirstOrDefault() in your code to determine of there is a record existing in your list. 您可以考虑在代码中使用FirstOrDefault()来确定列表中是否存在记录。 It will return an object or null if record doesn't exist. 如果记录不存在,它将返回一个对象或null。

And of course, don't forget your DBContext 当然,不要忘记您的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....
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM