Click here to Skip to main content
14,880,638 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am working on ASP.NET MVC application. I need to save the data to SQL Server.



This is my index view.

HTML
div class="row">
    <div class="col-sm-4">


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


            <div class="form-group">
                <label class="form-label">CAR NUMBER</label>
                <input type="text" id="carnum" name="carnum" class="form-control" placeholder="Enter Car Number" required>
            </div>

            <div class="form-group">
                <label class="form-label">CUSTOMER ID</label>
                <input type="text" id="custid" name="custid" class="form-control" placeholder="Enter Customer ID" required>
            </div>

            <div class="form-group">
                <label class="form-label">Date</label>
                <input type="text" id="rdate" name="rdate" class="form-control" placeholder="Date" required>
            </div>

            <div class="form-group">
                <label class="form-label">Days Elapsed</label>
                <input type="text" id="elapsed" name="elapsed" class="form-control" placeholder="Enter Days Elapsed" required>
            </div>

            <div class="form-group">
                <label class="form-label">FINE</label>
                <input type="text" id="fine" name="fine" class="form-control" placeholder="Enter Fine" required>
            </div>

            <div class="form-group">
                <input type="submit" value="Save" class="btn btn-success" />
            </div>

        }

    </div>
</div>


This is my controller.

C#
public ActionResult Save(returncar returncar)
        {

                if (ModelState.IsValid)
                {
                    db.returncars.Add(returncar);

                    var car = db.carregs.SingleOrDefault(e => e.carnum == returncar.carnum);
                    if (car == null)
                        return HttpNotFound("Car number not found");

                    car.availability = "Yes";
                    db.Entry(car).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            return View("~/Views/Return/Index.cshtml", returncar);
        }


What I have tried:

I tried coding all necessary parts and when I enter the data, the program says it is saved but when I check SQL Server Management Studio, nothing shows.
Posted
Updated 18-Jan-21 5:59am
v2
Comments
Sandeep Mewara 17-Jan-21 7:57am
   
Did you debug? Any error?

1 solution

Your code is okay try retrieving the data programatically and see if it returns null
And also for this case debugging the code will be helpful, it wil help you know what happens in each statement
   
v2
Comments
stoopeed 17-Jan-21 21:35pm
   
I changed my code to this:

public ActionResult Save(returncar returncars)
{
try
{
returncar ret = new returncar();

ret.id = returncars.id;
ret.carnum = returncars.carnum;
ret.custid = returncars.custid;
ret.rdate = returncars.rdate;
ret.elapsed = returncars.elapsed;
ret.fine = returncars.fine;

db.returncars.Add(returncars);
var car = db.carregs.SingleOrDefault(e => e.carnum == returncars.carnum);
if (car == null)
return HttpNotFound("Car number not found");




car.availability = "Yes";
db.Entry(car).State = EntityState.Modified;
db.SaveChanges();

int latestRetId = ret.id;

}
catch (Exception ex)
{
throw ex;
}



return RedirectToAction("Index");
}

It's already working. But rdate appears null. Can you also help me with this?
Mwasigala Caroline 2-Mar-21 4:43am
   
rdate appearing null could be caused by two things either the input name is mismatched or you didn't input the date

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900