简体   繁体   中英

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. 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:

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. Please help!

It doesn't look like you are querying the database, try changing your Details action to

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.

You could consider using FirstOrDefault() in your code to determine of there is a record existing in your list. It will return an object or null if record doesn't exist.

And of course, don't forget your 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....
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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