user2962142
user2962142

Reputation: 2066

Picking a row depending on 2 columns values

Alright so I'm trying to pick a row depending on 2 columns values, here's my code:

[HttpPost]
public ActionResult Index(Models.Seats ss)
{
   var bus = db.Seats.Where(u => u.BusID == ss.BusID && u.SeatID == ss.SeatID).FirstOrDefault();
   bus.SeatState = 1;
   db.SaveChanges();
   return View();
}

where db is the EF name, seats is the table name, and ss.BusID holds "1" and ss.SeatID holds "A2"..

heres a picture after executing the code :

enter image description here

my model code :

namespace MvcApplication12.Models
{
   using System;
   using System.Collections.Generic;

   public partial class Seats
   {
      public int BusID { get; set; }
      public string SeatID { get; set; }
      public Nullable<int> SeatState { get; set; }
   }
}

*I've tried ".Find()" and ".firstorDefault()" alone but still the same result.

So my problem is that I want to change the value where the Bus ID is 1 and Seat ID is A2, but as you can see, it changes the value for every row with BusID =1?

Upvotes: 2

Views: 89

Answers (1)

Vnuuk
Vnuuk

Reputation: 6527

Please could you try this way:

var busList = db.Seats.Where(u => u.BusID == ss.BusID && u.SeatID == ss.SeatID).ToList();

foreach(var bus in busList) 
   bus.SeatState = 1;

db.SaveChanges();

My code saves each row in you table that has necessary SeatID and BusID. Please let me know in case of.

Upvotes: 3

Related Questions