Questions tagged [modelbinders]

263 questions
46
votes
6 answers

MVC3 Non-Sequential Indices and DefaultModelBinder

Is it true that the default model binder in MVC 3.0 is capable of handling non-sequential indices (for both simple and complex model types)? I've come across posts that suggest it should, however in my tests it appears that it does NOT. Given post…
mindlessgoods
  • 1,135
  • 1
  • 11
  • 22
38
votes
3 answers

ASP.NET MVC - Mixing Custom and Default Model Binding

I have a type: public class IssueForm { Order Order {get; set;} Item Item {get; set;} Range Range {get; set;} } I created a custom model binder due to requirements on Order and Item, but Range could still use the Default Model…
anonymous
  • 6,825
  • 8
  • 47
  • 60
29
votes
3 answers

How to use Json.NET for JSON modelbinding in an MVC5 project?

I've been looking around the internet for an answer or example, but could not find one yet. I simply would like to change the default JSON serializer which is used to deserialize JSON while modelbinding to JSON.NET library. I've found this SO post,…
Zoltán Tamási
  • 12,249
  • 8
  • 65
  • 93
27
votes
5 answers

Why Two Classes, View Model and Domain Model?

I know it could be bad to use domain models as view models. If my domain model has a property named IsAdmin and I have a Create controller action to create users, someone could alter my form and get it to POST a IsAdmin=true form value, even if I…
enamrik
  • 2,292
  • 2
  • 27
  • 42
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
24
votes
4 answers

ASP.NET MVC posted file model binding when parameter is Model

Is there any way to get posted files () to take part in model binding in ASP.NET MVC without manually looking at the request context in a custom model binder, and without creating a separate action method which only takes a…
bzlm
  • 9,626
  • 6
  • 65
  • 92
23
votes
4 answers

Accept comma and dot as decimal separator

Model binding in ASP.NET MVC is great, but it follows locale settings. In my locale decimal separator is comma (','), but users use dot ('.') too, because they are lazy to switch layouts. I want this implemented in one place for all decimal fields…
artvolk
  • 9,448
  • 11
  • 56
  • 85
22
votes
7 answers

Why does ASP.NET MVC care about my read only properties during databinding?

Edit: Added bounty because I'm seeking an MVC3 solution (if one exists) other than this: DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false; I have a read only property on my 'Address' model 'CityStateZip'. It's…
Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
21
votes
5 answers

ASP.NET MVC - Alternative for [Bind(Exclude = "Id")]

Is there an alternative for [Bind(Exclude = "Id")] (Related Question) ? Could I write a model binder?
Rookian
  • 19,841
  • 28
  • 110
  • 180
21
votes
2 answers

Unit testing custom model binder in ASP.NET MVC 2

I've wrote custom model binder in project, that uses ASP.NET MVC 2. This model binder bind just 2 fields of model: public class TaskFormBinder : DefaultModelBinder { protected override void BindProperty(ControllerContext controllerContext, …
Mike G.
  • 700
  • 5
  • 22
21
votes
2 answers

ASP.net MVC v2 - Debugging Model Binding Issues - BUG?

I am having more than a little difficulty trying to debug why MVC is not binding correctly in a given case I have... Basically, I have my action which receives a complex object which in turn has a complex child object - Activity.Location.State…
vdh_ant
  • 12,720
  • 13
  • 66
  • 86
19
votes
3 answers

ASP.NET MVC - Custom Model Binder on Interface Type

I'm not sure if this behavior is expected or not, but it seems that custom model binding doesn't work when the binding is assigned to an interface type. Has anyone experimented with this? public interface ISomeModel {} public class SomeModel :…
Nathan Taylor
  • 24,423
  • 19
  • 99
  • 156
17
votes
1 answer

Setting ModelState values in custom model binder

I am using custom model binder in ASP.NET MVC 2 that looks like this: public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { if (controllerContext == null) { …
mare
  • 13,033
  • 24
  • 102
  • 191
17
votes
3 answers

Modelbinding for empty query string parameters in ASP.NET MVC 2

The behavior described here appears to now be the default for ASP.NET MVC 2 (at least for Preview 1). When modelbinding a querystring like this : ?Foo=&Bar=cat The following binding occurs (assuming you're binding to a model with 'Foo' and 'Bar'…
Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
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
1
2 3
17 18