This repository is created to facilitate the model binding of SmartEnum in dotnet MVC / Razor Pages forms. This issue is also created in SmartEnum github repository here
- Use the Model Binder Class from here
- Use the Model Binder Provider from here
- Add the following in
ConfigureServicesMethod ofStartUp.csof the Web Application project
services.AddRazorPages()
.AddMvcOptions(options =>
{
options.ModelBinderProviders.Insert(0, new SmartEnumBinderProvider());
});- Use the Enum options explicitly in the Razor page. Example can be seen here
<div class="form-group">
<label asp-for="Usr.Gender" class="control-label"></label>
<select asp-for="Usr.Gender" class="form-control">
@foreach (var item in GenderEnum.List)
{
<option value="@item.Name" selected="@(item.Name == Model.Usr.Gender.Name)">@item.Name</option>
}
</select>
<span asp-validation-for="Usr.Gender" class="text-danger"></span>
</div>- The main classes are located here
- You can just copy these files and use in your web application for smart enum model binding