Giter Site home page Giter Site logo

Comments (3)

ShawInnes avatar ShawInnes commented on May 26, 2024

Sorry this doesn't directly answer your question, but I've managed to find a workaround by making my [FromQuery] object use a string type... It's not as clean, but it does the job.

on my Controller:

    public async Task<IActionResult> GetPepVariants([FromQuery] GetAdminPepVariantsQuery request) =>

The request type:

public class GetAdminPepVariantsQuery
{
    ...
    public string? PepType { get; set; }
    ...
}

The EF query (or any other use)

query = query.Where(p => p.PepType == PepType.FromName(request.PepType, true));

from smartenum.

miroslavgrozdanovski avatar miroslavgrozdanovski commented on May 26, 2024

@laska-u, when the data is not really a JSON coming from a request body, the JsonConverters are not executed. I just encountered the same issue, and there are two options that worked for me:

  1. Use a custom TypeConverter as described in the Microsoft docs. Their example uses a GeoPoint type, but in our example with SmartEnums, you would just convert by calling FromValue(s).
  2. If you are using .NET 7, you can just add a TryParse implementation in your SmartEnum, and the framework will call it. Here is an example of that in the release notes of .NET 7 preview 3.

You can also use a custom model binder, but I didn't try that option as it's not needed.

@ardalis, should any of these options be added into the library? I'm open on contributing if you agree.

from smartenum.

ardalis avatar ardalis commented on May 26, 2024

@miroslavgrozdanovski Sure, that would be great. I probably need to improve the docs for SmartEnum some time as well, to cover things like this that aren't necessarily "in the box"...

from smartenum.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.