Questions tagged [modelbinders]

263 questions
15
votes
3 answers

ASP.Net Web Api not binding model on POST

I'm trying to POST JSON data to a Web Api method but the JSON data is not binding to the model. Here's my model: [DataContract] public class RegisterDataModel { [DataMember(IsRequired = true)] public String SiteKey { get; set; } …
Tom Schreck
  • 5,177
  • 12
  • 68
  • 122
14
votes
3 answers

Use custom ASP.NET MVC IValueProvider, without setting it globally?

I want to be able to grab keys/values from a cookie and use that to bind a model. Rather than building a custom ModelBinder, I believe that the DefaultModelBinder works well out of the box, and the best way to choose where the values come from would…
Oved D
  • 7,132
  • 10
  • 47
  • 69
13
votes
1 answer

How to Unit Test a custom ModelBinder using Moq?

I'm having some difficulty writing some Unit Tests to test a custom ModelBinder that I created. The ModelBinder I'm trying to Unit Test is the JsonDictionaryModelBinder that I posted here. The problem I'm having is getting the Mocking all setup…
Chris Pietschmann
  • 29,502
  • 35
  • 121
  • 166
12
votes
4 answers

ASP.NET MVC 2 - ViewModel Prefix

I want to use RenderPartial twice in my view with different models associated. The problem is that some properties are present in both models (nickname, password). They have no prefix, so even the id's or names are equal in the output. Now, if I…
Cosmo
  • 369
  • 1
  • 7
  • 16
12
votes
2 answers

What is the difference between BindProperty and SetProperty on IModelBinder

I'm creating a custom model binder in an Mvc application and I want to parse a string to an enumeration value and assign it to the model property. I have got it working overriding the BindProperty method, but I also noticed that there is a…
Andy McCluggage
  • 37,618
  • 18
  • 59
  • 69
11
votes
2 answers

Is there any way to disable the JSON ModelBinder in ASP.NET MVC 3 RC2?

In ASP.NET MVC 3 RC2, the default ModelBinder will automatically parse the request body if the Content-Type is set to application/json. Problem is, this leaves the Request.InputStream at the end of the stream. This means that if you try to read the…
Daniel T.
  • 37,212
  • 36
  • 139
  • 206
11
votes
5 answers

ASP.NET MVC - POST Action Method with Additional Parameters from URL

With ASP.net MVC is it possible to POST a form to a controller action which includes parameters not in the form, but from the URL? For example The Action method in GroupController: [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(int…
Karl
  • 659
  • 1
  • 10
  • 18
9
votes
2 answers

OnActionExecuting add to model before getting to action

I have the following: public override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); if (filterContext == null) { throw new…
user156888
9
votes
1 answer

Custom model binding, model state, and data annotations

I have a few questions regarding custom model binding, model state, and data annotations. 1) Is it redundant to do validation in the custom model binder if I have data annotations on my model, because that's what I thought the point of data…
9
votes
2 answers

ValueProvider does not contain a definition for TryGetValue

In my application, I am trying to split the Date and Time from and DateTime field so I can put a jQuery date picker on the date. I found Hanselman's code for splitting the DateTime, however I get a compile error on…
Mike Wills
  • 20,959
  • 28
  • 93
  • 149
9
votes
3 answers

Generic TimeSpan binding in Asp.NET MVC 2

I have an input form that is bound to a model. The model has a TimeSpan property, but it only gets the value correctly if I enter the time as hh:mm or hh:mm:ss. What I want is for it to capture the value even if it's written as hhmm or hh.mm or…
Carles Company
  • 7,118
  • 5
  • 49
  • 75
9
votes
3 answers

ASP.NET MVC 2 - Setting values on IValueProvider

I am attempting to upgrade my MVC 1 project to MVC 2 RC. We currently have a custom modelbinder that adds items to the ValueProvider (this worked when it was a dictionary). We then passed this off to the default modelbinder. However, IValueProvider…
Keith Rousseau
  • 4,435
  • 1
  • 22
  • 28
8
votes
2 answers

ASP.NET MVC Model Binding IList in an Editor Template

I am attempting to bind a list that is part of a larger view model without resorting to a custom model binder. When I use an editor template to build the list of inputs, the generated names are not in the correct format for the default binder to…
A Bunch
  • 373
  • 1
  • 4
  • 10
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
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
1
2
3
17 18