PolymorphicModelBinder 1.0.1
.NET 5.0
This package targets .NET 5.0. The package is compatible with this framework or higher.
.NET Standard 2.1
This package targets .NET Standard 2.1. The package is compatible with this framework or higher.
dotnet add package PolymorphicModelBinder --version 1.0.1
NuGet\Install-Package PolymorphicModelBinder -Version 1.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PolymorphicModelBinder" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PolymorphicModelBinder --version 1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: PolymorphicModelBinder, 1.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install PolymorphicModelBinder as a Cake Addin #addin nuget:?package=PolymorphicModelBinder&version=1.0.1 // Install PolymorphicModelBinder as a Cake Tool #tool nuget:?package=PolymorphicModelBinder&version=1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
PolymorphicModelBinder
A polymorphic model binder for ASP.net core. Bind to the type you want!
Getting Started
PolymorphicModelBinder can be installed using the Nuget package manager or the dotnet CLI.
dotnet add package PolymorphicModelBinder
Usage
// Models
public abstract class Pet
{
public string Name { get; set; }
}
public class Dog : Pet
{
public bool CanBark { get; set; }
}
public class Cat : Pet
{
public bool CanMeow { get; set; }
}
// Add polymorphic model binder
builder.Services.AddPolymorphicModelBinder(options =>
{
options.Add<Pet>(polymorphicBuilder =>
{
polymorphicBuilder.AddFromTypeInValue<Dog>();
polymorphicBuilder.AddFromTypeInValue<Cat>();
});
});
// Controller
public class HomeController : Controller
{
[HttpGet]
public IActionResult Index(string type = "Dog")
{
Pet pet = type switch
{
"Dog" => new Dog(),
"Cat" => new Cat(),
_ => throw new ArgumentOutOfRangeException(nameof(type))
};
return View(new SampleViewModel(pet));
}
[HttpPost]
public IActionResult Index(SampleViewModel viewModel) => View(viewModel);
}
// View
@using PolymorphicModelBinder.Html
@model SampleViewModel
@using (Html.BeginForm(FormMethod.Post))
{
@Html.PolymorphicTypeInValueFor(x => x.Pet)
@Html.PolymorphicEditorFor(x => x.Pet)
<button type="submit" class="btn btn-primary btn-lg">Submit</button>
}
Samples
- Polymorphic model as root model
- Polymorphic model as property
- Dynamic polymorphic list
- Polymorphic model with discriminator field
Roadmap
If you want to see a new feature feel free to create a new issue. Here are some features which are planned when there is enough enthusiasm for this feature.
- Basic implementation with a type in value and discriminator.
- Allow to use
@Html.PolymorphicEditorFor()
forIEnumerable
models. - Probably better documentation for usages.
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
License
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.1
- Microsoft.AspNetCore.Mvc (>= 2.2.0)
- Microsoft.AspNetCore.Mvc.Abstractions (>= 2.2.0)
- Microsoft.Extensions.DependencyInjection (>= 6.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Hosting (>= 6.0.1)
- Microsoft.Extensions.Options (>= 6.0.0)
-
net5.0
- Microsoft.AspNetCore.Mvc (>= 2.2.0)
- Microsoft.AspNetCore.Mvc.Abstractions (>= 2.2.0)
- Microsoft.Extensions.DependencyInjection (>= 6.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Hosting (>= 6.0.1)
- Microsoft.Extensions.Options (>= 6.0.0)
-
net6.0
- Microsoft.AspNetCore.Mvc (>= 2.2.0)
- Microsoft.AspNetCore.Mvc.Abstractions (>= 2.2.0)
- Microsoft.Extensions.DependencyInjection (>= 6.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Hosting (>= 6.0.1)
- Microsoft.Extensions.Options (>= 6.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.