0

i am creating a simple car retail system project in asp.net mvc.i had a problem is when the customer retuns a car avalablie column need to be changes yes.

return

id  carno   custid  date       elap fine
1   A0002   1   2019-11-29     11   1100

car

id  carno   make    model   Available
1   A0001   honda   viff    yes
2   A0002   Honda   uii     no 

when the customer return the car. return table save the record success fully. i don't how to update to car table Available no change need to be yes.

i tried the code. return car save

 public ActionResult Save(returncar recar)
 {
      if (ModelState.IsValid)
      {
           db.returncars.Add(recar);
           db.SaveChanges();
           return RedirectToAction("Index");

      }

      return View(recar);
 }

Form Design

<div class="row">
    <div class="col-sm-8">

        @using (Html.BeginForm("Save", "return", FormMethod.Post, new { id = "popupForm" }))
        {
            <div>
                <h4> Registation</h4>
            </div>


            <div class="card-action">
                <label class="form-label">Car ID</label>
                  <input type="text" id="carno" name="carno" class="form-control" placeholder="carno" required />
            </div>



            <div class="card-action">

                <label class="form-label">Customer ID</label>

                <input type="text" id="custid" name="custid" class="form-control" placeholder="Customer ID" required />

            </div>



            <div class="card-action">

                <label class="form-label">Date</label>

                <input type="text" id="date" name="date" class="form-control" placeholder="Date" required />

            </div>


             <div class="card-action">

                <label class="form-label">Days Elapsed</label>

                 <input type="text" id="elap" name="elap" class="form-control" placeholder="Date" required />

            </div>


             <div class="card-action">

                <label class="form-label">Fine</label>

                 <input type="text" id="fine" name="fine" class="form-control" placeholder="Fine" required />

            </div>


            <div class="card">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>

        }
    </div>

</div>
2
  • 1
    You can check for if the car exists in the db and then update the existing object like db.returncars.Where(x=>x.id == recar.id).FirstOrDefault(). This will give you the car object if it exists and then you can update the values Commented Dec 10, 2019 at 3:37
  • yes it there carregs table name. db.carregs.Where(x => x.id == recar.id).FirstOrDefault(); this how have to write Commented Dec 10, 2019 at 3:47

2 Answers 2

1

According to your question, it seems that you want to update Car Available attribute to true when the Car returns.

So simple you can find the Car using carno and if exists update its status. Here is how you can do this.

public ActionResult Save(returncar recar)
    {
        if (ModelState.IsValid)
        {
            db.returncars.Add(recar);
            //I realized that the carno is unique
            var car= db.cars.SingleOrDefault(e => e.carno == recar.carno);
            if(car == null)
            return HttpNotFound("CarNo is not valid");

            car.Available = true;
            db.Entry(car).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");

        }

      return View(recar);
    }

So this will update your Car Available attribute to true when a Car will be returned.

Sign up to request clarification or add additional context in comments.

3 Comments

car.Available = true; sir line error convert to bool to string
Thanksssssssssssssssssssssssssssss
@Javafiver I've updated car.Available = true; I was assuming it a string.
0

Please try this:

public class Return
{
   [Key]
   public int Id {get; set;}
   public int carno {get; set;}

   public string carno {get; set;}   
   // Other code goes here.
}

public class car
{
   [Key]
   public int Id {get; set;}
   public string carno {get; set;}
   public string Available {get; set;}
   // Other code goes here.
}

and Modify your Controller Action Save method:

var car = db.cars.FirstOrDefault(c => c.carno == recar.carno);

if (car != null) {
    db.returncars.Add(recar);
    car.Available = "yes";
    ((DbEntryEntity)car).State = EntityState.Modified;  
    db.SaveChanges();
}
else {
  // Handle it
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.