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
107
votes
5 answers

ASP.NET MVC A potentially dangerous Request.Form value was detected from the client when using a custom modelbinder

Getting the error here: ValueProviderResult value = bindingContext.ValueProvider.GetValue("ConfirmationMessage"); How do I allow on a selection of values only? i.e. [ValidateInput(false)] public object BindModel(ControllerContext controllerContext,…
D-W
  • 5,201
  • 14
  • 47
  • 74
38
votes
4 answers

Custom model binder for a property

I have the following controller action: [HttpPost] public ViewResult DoSomething(MyModel model) { // do something return View(); } Where MyModel looks like this: public class MyModel { public string PropertyA {get; set;} public…
rrejc
  • 2,682
  • 3
  • 26
  • 33
25
votes
5 answers

Custom DateTime model binder in Asp.net MVC

I would like to write my own model binder for DateTime type. First of all I'd like to write a new attribute that I can attach to my model property like: [DateTimeFormat("d.M.yyyy")] public DateTime Birth { get; set,} This is the easy part. But the…
Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
25
votes
2 answers

ASP.Net Web API model binding not working like it does in MVC 3

I was under the impression that model binding in the ASP.Net Web API was supposed to support binding with the same minimum level of functionality supported by MVC. Take the following controller: public class WordsController : ApiController { …
21
votes
1 answer

Custom Model Binder for ASP.NET MVC on GET request

I've created a custom MVC Model Binder which gets called for every HttpPost that comes into the server. But does not get called for HttpGet requests. Should my custom model binder get called during a GET? If so, what did I miss? If not, How can I…
Niels Filter
  • 4,430
  • 3
  • 28
  • 42
16
votes
2 answers

.NET core custom and default binding combined

I'm creating a custom model binder for a view model, implementing IModelBinder I have a lot of properties in my view model, the majority of which do not need any custom binding. Rather than explicitly set all of the property values on my model…
16
votes
2 answers

ASP.NET MVC2 - Custom Model Binder Examples

I am trying to find some examples of building a custom model binder for a unique binding scenario I need to handle, but all of the articles I found were for older versions of MVC which are no longer relevant in MVC2. I've been referencing the…
Nathan Taylor
  • 24,423
  • 19
  • 99
  • 156
14
votes
2 answers

asp.net core custom model binder just for one property

I have a simple model for my asp.net core controller: [HttpPost] public async Task AddCourse([FromBody]CourseDto dto) { var response = await _courseService.AddCourse(dto); return response; } My model is : public class…
11
votes
3 answers

Change culture before ModelBinder is used

I want to create a website in different languages. I already read that I could create an ActionFilter, but I have a litte problem: I had to create a custom ModelBinder in order to work with english and german number formats (123,456,789.1 vs.…
Christopher
  • 2,005
  • 3
  • 24
  • 50
10
votes
3 answers

Custom Model Binder Not Validating Model

I started to play around with knockout.js and in doing so I used the FromJsonAttribute (created by Steve Sanderson). I ran into an issue with the custom attribute not performing model validation. I put together a simple example-- I know it looks…
ek_ny
  • 10,153
  • 6
  • 47
  • 60
10
votes
2 answers

Polymorphic model binding in AspNet Core WebApi?

anyone has a working example of a custom model binding with polymorphic model binding? I'm trying this example (which is for Mvc not Api projects) with a web api project but it's not working for API projects. I think some steps are missing in terms…
kovac
  • 4,945
  • 9
  • 47
  • 90
10
votes
2 answers

MVC 6 Custom Model Binder with Dependency Injection

Right now my ViewModel looks like this: public class MyViewModel { private readonly IMyService myService; public ClaimantSearchViewModel(IMyService myService) { this.myService = myService; } } My Controller that consumes…
Serj Sagan
  • 28,927
  • 17
  • 154
  • 183
9
votes
1 answer

Custom model binding through body in ASP.Net Core

I would like to bind an object in a controller through the body of a HTTP Post. It works like this public class MyModelBinder : IModelBinder { public Task BindModelAsync(ModelBindingContext bindingContext) { if…
rst
  • 2,510
  • 4
  • 21
  • 47
9
votes
2 answers

ASP.NET MVC Model Binder with Global Number Formats

The default model binder is returning errors for properties that are of type double when my application is being used in countries that use different number formatting for decimals (e.g. 1.2 = 1,2). The culture of the site is set conditionally in my…
8
votes
1 answer

Problem with double values binding

In my project I want to allow users input double values in 2 formats: with using ',' or '.' as delimiter (I'm not interested in exponential form). By default value with delimiter '.' don't work. I want this behavior works for all double properties…
1
2 3
11 12