In this article I will explain with an example, how to keep DropDownList value selected after Form Submit (Post) in ASP.Net Core Razor Pages.
Database
I have made use of the following table Customers with the schema as follows. CustomerId is an Auto-Increment (Identity) column.
I have already inserted few records in the table.
Note: You can download the database table SQL by clicking the download link below.
Namespaces
You will need to import the following namespace.
using Microsoft.AspNetCore.Mvc.Rendering;
Model
The Model class consists of the following two properties.
Note: In this article, only two Columns will be used and hence two properties are added to the class.
public class Customer
{
public int CustomerId { get; set; }
public string Name { get; set; }
}
Database Context
Once the Entity Framework is configured and connected to the database table, the Database Context will look as shown below.
using Microsoft.EntityFrameworkCore;
using Razor_DropDownList_Selected_Submit.Models;
namespace Razor_DropDownList_Selected_Submit
{
public class DBCtx : DbContext
{
public DBCtx(DbContextOptions<DBCtx> options) : base(options)
{
}
public DbSet<Customer> Customers { get; set; }
}
}
Razor PageModel (Code-Behind)
The PageModel consists of two Handler methods.
Handler method for handling GET operation
Inside this Handler method, the PopulateDropDownList method is called which fetches the records from the Customers table using Entity Framework and then the records copied to SelectList class object and assigned to the public property Customers, which is used for populating DropDownList in ASP.Net Core Razor Pages.
Handler method for handling Button Click and POST operation
This Handler method handles the POST call when the Submit Button is clicked and the Form is submitted.
Inside this Handler method, the DropDownList is again populated using the PopulateDropDownList method.
The customerId value is fetched and is set into a ViewData object which will be later used to select the DropDownList item.
public class IndexModel : PageModel
{
private DBCtx Context { get; }
public IndexModel(DBCtx _context)
{
this.Context = _context;
}
public SelectList Customers { get; set; }
public void OnGet()
{
this.PopulateDropDownList();
}
public void PopulateDropDownList()
{
this.Customers = new SelectList(this.Context.Customers, "CustomerId", "Name");
}
public void OnPostSubmit(string customerId)
{
this.PopulateDropDownList();
ViewData["SelectedValue"] = customerId;
}
}
Razor Page (HTML)
The HTML of Razor Page consists of an HTML Form with a DropDownList and a Submit Button.
The Submit Button has been set with the POST Handler method using the asp-page-handler attribute.
Note: In the Razor PageModel, the Handler method name is OnPostSubmit but here it will be specified as Submit when calling from the Razor HTML Page.
The Model data has been assigned to the DropDownList using the asp-items Tag Helpers attribute.
When the Submit Button is clicked, the Form gets submitted and the selected customerId value is sent to the Razor PageModel.
Finally, the ViewData object returned from the PageModel is checked for NULL and if it is not NULL then the customerId value of the selected Customer is used to select the DropDownList item using jQuery.
@page
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@model Razor_DropDownList_Selected_Submit.Pages.IndexModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<form method="post">
<select id="ddlCustomers" name="CustomerId" asp-items="@Model.Customers">
<option value="0">--Select Customer--</option>
</select>
<br />
<br />
<input type="submit" value="Submit" asp-page-handler="Submit" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
@if (ViewData["SelectedValue"] != null)
{
<script type="text/javascript">
$(function () {
$(this).find("#ddlCustomers").val('@ViewData["SelectedValue"]');
});
</script>
}
</form>
</body>
</html>
Screenshot
Downloads