PolymorphicModelBinder 1.0.1

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                
#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                

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

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() for IEnumerable 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

MIT

Product 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.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 617 4/13/2022
1.0.0 562 4/13/2022
0.0.9 570 4/9/2022