Hi @Sherpa,
1.Modify the Register.cshtml.cs
file
public class RegisterModel : PageModel
{
[BindProperty]
public InputModel Input { get; set; }
public RegistrationVM RegistrationVM { get; set; }
public class InputModel
{
// Existing properties
public string Title { get; set; }
public string Salutation { get; set; }
}
public async Task OnGetAsync()
{
// Populate the RegistrationVM with SelectListItems
RegistrationVM = new RegistrationVM
{
TitleList = new List<SelectListItem>
{
new SelectListItem { Value = "title1", Text = "title1" },
new SelectListItem { Value = "title2", Text = "title2" },
new SelectListItem { Value = "title3", Text = "title3" }
},
SalutationList = new List<SelectListItem>
{
new SelectListItem { Value = "Salutation1", Text = "Salutation1" },
new SelectListItem { Value = "Salutation2", Text = "Salutation2" }
}
};
}
public async Task<IActionResult> OnPostAsync()
{
// Handle registration logic
return RedirectToPage("Index");
}
}
2.Modify the Register.cshtml
file
@page
@model RegisterModel
@{
ViewData["Title"] = "Register";
}
<h1>@ViewData["Title"]</h1>
<form method="post">
<div>
<label asp-for="Input.Title"></label>
<select asp-for="Input.Title" asp-items="Model.RegistrationVM.TitleList"></select>
<span asp-validation-for="Input.Title"></span>
</div>
<div>
<label asp-for="Input.Salutation"></label>
<select asp-for="Input.Salutation" asp-items="Model.RegistrationVM.SalutationList"></select>
<span asp-validation-for="Input.Salutation"></span>
</div>
<button type="submit">Register</button>
</form>
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
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,
Rena