O. Haifa O. Haifa - 28 days ago 6
C# Question

Adding to a table using MVC 5, and reset Id after deleting records

I have 2 questions:

First I wrote a code to add doctors to database, at the beginning the code worked fine but after adding validation the doctor information does not added to table.. when I press add button the URL goes to: /Doctors/Save
Why the data does not saved in database? And why after I pressed Add button the program does not redirect me to Add view?

In controller there

Add()
action is for the Add view which contains the adding form..
Save()
action is for the
Html.BeginForm("Save","Doctors")
that is inside the
Add
view..

Here is my code:

public ActionResult Add()
{ return View();
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Save(Doctor doctor)
{
if (!ModelState.IsValid)
{
var viewModel = new DoctorViewModel
{
Doctor = doctor

};
return View("Add", viewModel);
}

if(doctor.Id == 0) {
_context.Doctors.Add(doctor);
_context.SaveChanges();
return Redirect("Add");
}
else
{
var doctorInDb = _context.Doctors.Single(c => c.Id == doctor.Id);
doctorInDb.Name = doctor.Name;
doctorInDb.Symbol = doctor.Symbol;
doctorInDb.Office = doctor.Office;
doctorInDb.Phone = doctor.Phone;
doctorInDb.Email = doctor.Email;
_context.SaveChanges();
return Redirect("Add");
}

}


Add view:

@model Project.ViewModels.DoctorViewModel

@{
ViewBag.Title = "Add";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Adding a Doctor</h2>

@using (Html.BeginForm("Save", "Doctors"))
{
<div class="form-group">

@Html.LabelFor(m => m.Doctor.Name)
@Html.TextBoxFor(m => m.Doctor.Name , new { @class = "form-control"})
@Html.ValidationMessageFor(m => m.Doctor.Name)
</div>

<div class="form-group">

@Html.LabelFor(m => m.Doctor.Symbol)
@Html.TextBoxFor(m => m.Doctor.Symbol, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Doctor.Symbol)
</div>
<div class="form-group">
@Html.LabelFor(m => m.Doctor.Phone)
@Html.TextBoxFor(m => m.Doctor.Phone, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Doctor.Phone)
</div>


<div class="form-group">
@Html.LabelFor(m => m.Doctor.Office)
@Html.TextBoxFor(m => m.Doctor.Office, new { @class = "form-control" })
</div>



<div class="form-group">
@Html.LabelFor(m => m.Doctor.Email)
@Html.TextBoxFor(m => m.Doctor.Email, new { @class = "form-control" })
</div>

@Html.HiddenFor(m => m.Doctor.Id)
@Html.AntiForgeryToken()
<button type="submit" class="btn btn-primary"> <h5> Add </h5> </button> @Html.ActionLink("Go to doctors list", "DoctorList")
}

@section scripts{
@Scripts.Render("~/bundles/jqueryval")

}


Second question:

Can I reset the Id, after deleting all records from table, from the controller?

Here is my reset code:

public ActionResult Reset()
{
_context.PSRs.RemoveRange(_context.PSRs);

_context.SaveChanges();
return Redirect("PSR");
}


Thanks!

Answer

Well, for what you have said I believe that:

First I wrote a code to add doctors to database, at the beginning the code worked fine but after adding validation the doctor information does not added to table.. when I press add button the URL goes to: /Doctors/Save Why the data does not saved in database?

It looks like the reason the data is not saved in database is because your Model is invalid:

    if (!ModelState.IsValid)
    {
        var viewModel = new DoctorViewModel
        {
            Doctor = doctor

        };
        return View("Add", viewModel);
    }

So you can debug your code to see the reason why it is invalid. If you want, you can insert this code:

var message = string.Join(" | ", ModelState.Values
    .SelectMany(v => v.Errors)
    .Select(e => e.ErrorMessage));

before var viewModel and a breakpoint on it so you don´t need to navigate in the properties during debug.

And why after I pressed Add button the program does not redirect me to Add view?

In controller there Add() action is for the Add view which contains the adding form.. Save() action is for the Html.BeginForm("Save","Doctors") that is inside the Add view..

You are not being redirect to the "Add view" because you are not saving doctor successfully (ModelState is invalid) therefore it just renders Add view (URL keeps as /Doctors/Save) but not redirect to it.

Second question:

Can I reset the Id, after deleting all records from table, from the controller?

Don´t know the answer for this one, maybe you should have splitted the question in two, so people can answer to one question but not to the other one.

Regards,

Comments