Questions tagged [custom-model-binder]

The custom binding class needs to inherit form IModelBinder. Here we capture the current request and extract the Form fields individually. Then we can manipulate these fields any way we like.

Useful links

176 questions
8
votes
1 answer

Binding error in custom model minder removes the value the user inputted

I'm using ASP.NET MVC 3 RTM, and I have a view model like this: public class TaskModel { // Lot's of normal properties like int, string, datetime etc. public TimeOfDay TimeOfDay { get; set; } } The TimeOfDay property is a custom struct I have,…
René
  • 9,880
  • 4
  • 43
  • 49
8
votes
1 answer

Custom DateTime model binder in ASP.NET Core 1 (RTM)

I'm writing and ASP.NET Core 1 application, which has a Web API Controller where supplier will post data. This is a simplified version of the Model class to which I will bind the incoming data: public class Lead { public int SupplierId { get;…
8
votes
3 answers

ASP.Net MVC ModelBindingContext class-- how are its model values populated?

I'm scratching my head a bit at how model binders do their work in ASP.Net MVC. To be specific, the BindModel() method has a ModelBindingContext parameter that holds the model name and type, but I don't understand how the ModelBindingContext…
larryq
  • 15,713
  • 38
  • 121
  • 190
8
votes
3 answers

How to use a DI / IoC container with the model binder in ASP.NET MVC 2+?

Let's say I have an User entity and I would want to set it's CreationTime property in the constructor to DateTime.Now. But being a unit test adopter I don't want to access DateTime.Now directly but use an ITimeProvider : public class User { …
8
votes
1 answer

ASP.NET MVC - Custom model binder able to process arrays

I need to implement a functionality to allow users to enter price in any form, i.e. to allow 10 USD, 10$, $10,... as input. I would like to solve this by implementing a custom model binder for Price class. class Price { decimal Value; int ID; }…
Marek
  • 10,307
  • 8
  • 70
  • 106
8
votes
1 answer

Why can't I add my custom model binder in my global.asax folder?

I see examples all over the net of people setting up their custom model binders like this: // global.asax protected void Application_Start() { ModelBinders.Binders.Add(typeof(YourModel), new YourBinder()); } But when I try that, it doesn't…
Michael Haren
  • 105,752
  • 40
  • 168
  • 205
7
votes
3 answers

Inject a dependency into a custom model binder and using InRequestScope using Ninject

I'm using NInject with NInject.Web.Mvc. To start with, I've created a simple test project in which I want an instance of IPostRepository to be shared between a controller and a custom model binder during the same web request. In my real project, I…
7
votes
2 answers

Deserialize enums using the EnumMember value in AspNet [FromQuery] model binding

I have an endpoint in .NET 6 Microsoft.NET.Sdk.Web project that deserialize query strings into a .NET object by using the standard [FromQuery] [Route("[controller]")] public class SamplesController : ControllerBase { [HttpGet] public…
7
votes
2 answers

Custom model binder not called when type is nullable

I have a custom struct called TimeOfDay which is used in a view model like this: public class MyViewModel { public TimeOfDay TimeOfDay { get; set; } } I have created a custom model binder called TimeOfDayModelBinder and registered it in…
René
  • 9,880
  • 4
  • 43
  • 49
6
votes
1 answer

Custom model binder not firing for a single property in ASP.NET Core 2

I already have tried this, but I don't think it's my case. This doesn't work neither. I'm using ASP.NET Core 2 Web API. I just created a dummy model binder (what it does doesn't matter for now): public class SanitizeModelBinder : IModelBinder { …
6
votes
2 answers

How to write unit test for custom model binder in ASP.net core

I have written custom model binder for a property. Now I am trying to write unit test for the same but not able to create object for model binder. Can anyone help me ? Below is the code for which I have to write test. public class…
megha
  • 81
  • 1
  • 4
6
votes
1 answer

Missing TaskCache class in .NET Core 2

I have created a Custom Model Binder in .NET Core 1.x . It works well as shown in the official tutorial. Once updated to 2.0, I can't compile it anymore. In the old version it was return TaskCache.CompletedTask; Now the TaskCache static class…
norbinto
  • 77
  • 1
  • 6
6
votes
3 answers

Creating dynamic forms with .net.core

I have a requirement to have different forms for different clients which can all be configured in the background (in the end in a database) My initial idea is to create an object for "Form" which has a "Dictionary of FormItem" to describe the form…
6
votes
1 answer

How to call Default Model Binding from custom binder in WebAPI?

I have a custom model binder in WebAPI that uses the following method from the `Sytem.Web.Http.ModelBinding' namespace which is the correct namespace for creating custom model binders for Web API: public bool BindModel(HttpActionContext…
atconway
  • 20,624
  • 30
  • 159
  • 229
6
votes
1 answer

Create Property Model Binder in Web API

I have created ModelBinder for a complex class. I would like to reuse this ModelBinder on a property. So is it possible to use ModelBinder on a property in Web API. I'm searching for an sample implementation which provides property binder like in an…
mehul9595
  • 1,925
  • 7
  • 32
  • 55
1
2
3
11 12