c# - Adding to a table using MVC 5, and reset Id after deleting records -
i have 2 questions:
first wrote code add doctors database, @ beginning code worked fine after adding validation doctor information not added table.. when press add button url goes to: /doctors/save why data not saved in database? , why after pressed add button program not redirect me add view?
in controller there add()
action add view contains adding form.. save()
action html.beginform("save","doctors")
inside add
view..
here 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 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 doctors list", "doctorlist") } @section scripts{ @scripts.render("~/bundles/jqueryval") }
second question:
can reset id, after deleting records table, controller?
here reset code:
public actionresult reset() { _context.psrs.removerange(_context.psrs); _context.savechanges(); return redirect("psr"); }
thanks!
well, have said believe that:
first wrote code add doctors database, @ beginning code worked fine after adding validation doctor information not added table.. when press add button url goes to: /doctors/save why data not saved in database?
it looks reason data not saved in database because model invalid:
if (!modelstate.isvalid) { var viewmodel = new doctorviewmodel { doctor = doctor }; return view("add", viewmodel); }
so can debug code see reason why invalid. if want, can insert code:
var message = string.join(" | ", modelstate.values .selectmany(v => v.errors) .select(e => e.errormessage));
before var viewmodel
, breakpoint on don´t need navigate in properties during debug.
and why after pressed add button program not redirect me add view?
in controller there add() action add view contains adding form.. save() action html.beginform("save","doctors") inside add view..
you not being redirect "add view" because not saving doctor (modelstate invalid) therefore renders add view (url keeps /doctors/save) not redirect it.
second question:
can reset id, after deleting records table, controller?
don´t know answer one, maybe should have splitted question in two, people can answer 1 question not other one.
regards,
Comments
Post a Comment