'How do I make a simple asp.net Core 6 page that sends the form data back to itself for modification with the answer?
I am wanting to replace windows forms small projects with ASP.net core 6. To do that I need to be able to send the form data back to the same form page so that it can be updated on the fly. So the form still holds the data when the page reloads, and holds the answer in a section below it.
I don't need the usual Database, and CRUD structure.
That way the user can refine it for what they want.
I think its something to do with [bindproperty].
Here is my working so far....
Main form
@page
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
<form>
<div class="mb-3">
<div class="row g-3">
<div class="col">
<label>Enter the Room Width</label>
<input asp-for="@Model.carpet.RoomWidth" type="number" class="form-control w-25" >
</div>
<div class="col">
<label>Enter the Room Length</label>
<input asp-for="@Model.carpet.RoomLength" type="number" class="form-control w-25" >
</div>
<div class="col">
<label>Enter the Carpet Type</label>
<select asp-for="@Model.carpet.CarpetType" asp-items="@Model.carpetTypesList"></select>
</div>
</div>
<div class="form-check">
<input class="form-check-input" asp-for="@Model.carpet.InstallationCost">
<label class="form-check-label" >Do you want us to install the Carpet?</label>
</div>
<div class="form-check">
<input class="form-check-input" asp-for="@Model.carpet.UnderlayCost">
<label class="form-check-label" >Do you want Underlay for your Carpet?</label>
</div>
</div>
<button type="submit" class="btn btn-primary">Submit Your Carpet Details</button>
</form>
This is the back end. The calculations are all in CarpetOperations, and Carpet is the model.
public class IndexModel : PageModel
{
[BindProperty]
public Carpet carpet { get; set; }
public List<SelectListItem> carpetTypesList { get; } = new List<SelectListItem>
{
new SelectListItem { Value = "1", Text = "Cheap" },
new SelectListItem { Value = "2", Text = "Home" },
new SelectListItem { Value = "3", Text = "Luxurious" },
};
public CarpetOperations carpetOperations { get; set; }
private readonly ILogger<IndexModel> _logger;
public IndexModel(ILogger<IndexModel> logger)
{
_logger = logger;
}
public void OnGet()
{
}
Thanks for your help.
Solution 1:[1]
Solved it!
For anyone who ever sees this in the future
[BindProperty(SupportsGet = true)]
public Carpet? carpet { get; set; }
and do whatever you need in the OnPostAsync
public async Task<IActionResult> OnPostAsync()
{
if (ModelState.IsValid)
{
carpet.InstallationCost = 20;
carpet.UnderlayCost = 20;
carpet.RoomArea = Convert.ToSingle(carpet.RoomWidth) * Convert.ToSingle(carpet.RoomLength);
carpet.FinalCost = carpetOperations.TotalInstallCost(carpet);
}
return Page();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | netchicken |
