0
I have an ASP.NET Core MVC web project with 4 pages.
The first page is a kind of login. Two text fields with a Next button. The second page shows data about the logged in person and contains two more text fields (mail address and date). Both of them must be filled.
On the page there is a Next button. If you click on this button, the personal data and the values from the two text fields should be displayed on the 3 page overview page (readonly fields). If you recognize an error from the two text fields, then you can navigate back to page 2 via the Back button and correct it.
Navigating back now works. What does not work yet is that the model is passed on to the last page SendData. Returning to the previous page works.
The model is declared and in the .cshtml I have a tag around both buttons.
This is what they look like.
@model PersonDataModel
<form asp-action="PersonData" asp-controller="Person" method="post" class="needs-validation" novalidate>
<div class="card">
<div class="card-header">
@HtmlLocalizer["Personvalue"]
</div>
<div class="card-body">
<div class="form-group row">
<label class="col-sm-3 col-form-label">@HtmlLocalizer["Person"]</label>
<div class="col-sm-3">
@Html.TextBoxFor(model => model.PersonName, new { @class = "form-control", @readonly = "readonly" })
</div>
<div class="col-sm-6">
@Html.TextBoxFor(model => model.PersonStreet, new { @class = "form-control", @readonly = "readonly" })
</div>
</div>
.... more code ....
<input type="hidden" name="back" value="true" />
<button type="submit" class="btn btn-primary">@HtmlLocalizer["Back"]</button>@*go back to PersonData site with actual model values*@
</form>
<form asp-action="Overview" asp-controller="Person" method="post" class="needs-validation" novalidate>
<button type="submit" class="btn btn-primary">@HtmlLocalizer["Next"]</button> @*go forward to Overview.cshtml*@
</form>
What else do I have to do so that the model is also passed on to the next page?
A model with empty values is displayed in the controller
public IActionResult Login()
{
ViewData["Title"] = _localizer["Login"];
return View();
}
public IActionResult PersonData()
{
ViewData["Title"] = _localizer["Persondata"].Value;
model.PersonName = "Firstname Lastname";
model.Street= "Street123";
model.Zip = "12345";
model.City= "Tokio";
return View(model);
}
[HttpPost]
public ActionResult PersonData(PersonDataModel personDataModel)
{
ViewData["Title"] = _localizer["Persondata"].Value;
if (ModelState.IsValid)
{
return RedirectToAction("Overview", personDataModel);
}
return View("Persondata", personDataModel);
}
public IActionResult Overview(PersonDataModel personDataModel)
{
ViewData["Title"] = _localizer["Overview"].Value;
model = persondataModel;
return View("Overview", personDataModel);
}
public IActionResult SendData()
{
ViewData["Title"] = _localizer["SendData"].Value;
return View();
}
[HttpPost]
public IActionResult SendData(PersonDataModel personDataModel)
{
ViewData["Title"] = _localizer["SendData"].Value;
return View();
}
Thank you
This button doesn't send any model data to the server because the form has no fields and no data:
<form asp-action="Overview" asp-controller="Person" method="post" class="needs-validation" novalidate>
<button type="submit" class="btn btn-primary">@HtmlLocalizer["Next"]</button> @*go forward to Overview.cshtml*@
</form>
The only element in this entire form is the submit button.
It looks like you intended to add the submit button to the previous form? For example:
<button type="submit" class="btn btn-primary">@HtmlLocalizer["Back"]</button>@*go back to PersonData site with actual model values*@
<button type="submit" class="btn btn-primary">@HtmlLocalizer["Next"]</button> @*go forward to Overview.cshtml*@
</form>
If you want that submit button to go to a different action method, specify it on the button:
<button type="submit" class="btn btn-primary">@HtmlLocalizer["Back"]</button>@*go back to PersonData site with actual model values*@
<button type="submit" asp-action="Overview" asp-controller="Person" class="btn btn-primary">@HtmlLocalizer["Next"]</button> @*go forward to Overview.cshtml*@
</form>