Hi @Sammani Palansuriya ,
If user clicks ok only it will save to Database
If user clicks on back button on confirmation page we need to show the changes made in the form
Issue we have is when we pass the model from save form to confirmation page, we show only some values in the page. and these are also set on HTML.Display for. So when back button is clicked data is not post back.
Based on your description, in the confirmation page, you could display the properties via the input text tag (set it as read only) and use tag helper to bind the value, then you can add two submit buttons to submit the form to different page.
Please refer the following sample:
The Controller has the following actions:
[HttpGet]
public IActionResult Create(Student student)
{
if (student == null)
student = new Student();
return View();
}
//
[HttpPost]
public IActionResult Confirmation(Student student)
{
return View(student);
}
//if user click the Save button in the Confirmantion Page, submit the form to this action to save data into database.
[HttpPost]
public IActionResult Save(Student student)
{
if (ModelState.IsValid)
{
//dosomething: store data into database.
}
return View(nameof(Index));
}
Create view page: After user click the Submit button, the form data will submit to the Confirmation action method.
@model Core3_1MVC.Models.Student
@{
ViewData["Title"] = "Create";
}
<h1>Create</h1>
<h4>Student</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Confirmation">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="LastName" class="control-label"></label>
<input asp-for="LastName" class="form-control" />
<span asp-validation-for="LastName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="FirstMidName" class="control-label"></label>
<input asp-for="FirstMidName" class="form-control" />
<span asp-validation-for="FirstMidName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="EnrollmentDate" class="control-label"></label>
<input asp-for="EnrollmentDate" class="form-control" />
<span asp-validation-for="EnrollmentDate" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
Confirmation view page: there have two submit button, and we use the formaction
attribute to submit the form to different action, and use the formmethod
attribute to set the submit method.
@model Core3_1MVC.Models.Student
@{
ViewData["Title"] = "Confirmation";
}
<h1>Confirmation</h1>
<div>
<h4>Are you sure you want to insert the following Student? </h4>
<hr />
<form action="">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="LastName" class="control-label"></label>
<input asp-for="LastName" class="form-control" readonly />
</div>
<div class="form-group">
<label asp-for="FirstMidName" class="control-label"></label>
<input asp-for="FirstMidName" class="form-control" readonly />
</div>
<div class="form-group">
<label asp-for="EnrollmentDate" class="control-label"></label>
<input asp-for="EnrollmentDate" class="form-control" readonly />
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" formaction="Save" formmethod="post" />
<input type="submit" value="Cancel" class="btn btn-primary" formaction="Create" formmethod="get" />
</div>
</form>
</div>
The result as below:
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion