modelbinders

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

旧巷老猫 提交于 2019-12-03 02:44:54
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 input stream using your own code, you first have reset it back to the beginning: // client sends HTTP request with Content-Type: application/json and a JSON // string in the body // requestBody is null because the stream is already at the end var requestBody = new StreamReader(Request.InputStream).ReadToEnd(); // resets the position back to the

How to Unit Test a custom ModelBinder using Moq?

血红的双手。 提交于 2019-12-02 22:10:26
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 using Moq. I keep getting Null Exceptions due to the HttpContextBase not being Mocked correctly. I think. Could someone help me figure out what I'm not doing correclty? Here's a sample of the Unit Test I'm trying to write that doesn't work: [TestMethod()] public void BindModelTest() { JsonDictionaryModelBinder target = new JsonDictionaryModelBinder();

MVVM and ModelBinders in the ASP.NET MVC Framework

不羁岁月 提交于 2019-12-02 18:55:11
I've got a series of views, each are typed to have their own ViewModel class which contains everything they need to display themselves, for example: public class CreateResourceViewModel { public Project Parent { get; set; } public SelectList Categories { get; set; } public Resource Resource { get; set; } } The post action method for this I'd like to use would look like this: [AcceptVerbs (HttpVerbs.Post)] public ActionResult Create (Resource resource) { // Update code... } Notice that the only object I'm interested in is the Resource property of the CreateResourceViewModel, not the

Custom Model Binder for decimals and integer: How to get the string value before MVC and do a smarter conversion

末鹿安然 提交于 2019-12-01 23:19:02
问题 I want to extend the default model binding to be more smart when dealing with numbers. The default works very bad when are commas and decimals points in the game. I was trying the do a new binder Public Class SmartModelBinder Inherits DefaultModelBinder Protected Overrides Sub SetProperty(controllerContext As ControllerContext, bindingContext As ModelBindingContext, propertyDescriptor As System.ComponentModel.PropertyDescriptor, value As Object) If propertyDescriptor.PropertyType Is GetType

Custom Model Binder for decimals and integer: How to get the string value before MVC and do a smarter conversion

孤街醉人 提交于 2019-12-01 22:13:36
I want to extend the default model binding to be more smart when dealing with numbers. The default works very bad when are commas and decimals points in the game. I was trying the do a new binder Public Class SmartModelBinder Inherits DefaultModelBinder Protected Overrides Sub SetProperty(controllerContext As ControllerContext, bindingContext As ModelBindingContext, propertyDescriptor As System.ComponentModel.PropertyDescriptor, value As Object) If propertyDescriptor.PropertyType Is GetType(Decimal) Or propertyDescriptor.PropertyType Is GetType(Decimal?) Then If value Is Nothing Then value = 0

ValueProvider does not contain a definition for TryGetValue

久未见 提交于 2019-12-01 15:48:54
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 bindingContext.ValueProvider.TryGetValue(modelName, out valueResult); . The error I get is: Error 3 'System.Web.Mvc.IValueProvider' does not contain a definition for 'TryGetValue' and no extension method 'TryGetValue' accepting a first argument of type 'System.Web.Mvc.IValueProvider' could be found (are you missing a using directive or an assembly reference?) C:\Documents and

ValueProvider does not contain a definition for TryGetValue

前提是你 提交于 2019-12-01 15:45:41
问题 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 bindingContext.ValueProvider.TryGetValue(modelName, out valueResult); . The error I get is: Error 3 'System.Web.Mvc.IValueProvider' does not contain a definition for 'TryGetValue' and no extension method 'TryGetValue' accepting a first argument of type 'System.Web.Mvc

MVC3 ModelBinding to a collection posted back with index gaps

*爱你&永不变心* 提交于 2019-12-01 06:20:36
I have a collection of objects on my Model that I'm rendering in a View by using EditFor function, and I have an EditorTemplate which is responsible for actually rendering each object. @Html.EditorFor(model => model.MyObjects) This has worked well for a while now, and when you check the html, my text boxes are prefixed with the model property, followed by the index of the collection they came from. <input class="text-box single-line" id="MyObjects_2__SomeProperty" name="MyObjects[2].SomeProperty" type="Text" value="" /> However I've recently started using the ShowForEdit and ShowForDisplay

Get FormCollection out controllerContext for Custom Model Binder

巧了我就是萌 提交于 2019-11-30 19:38:29
I had a nice function that took my FormCollection (provided from the controller). Now I want to do a model bind instead and have my model binder call that function and it needs the FormCollection. For some reason I can find it. I thought it would have been controllerContext.HttpContext.Request.Form Try this: var formCollection = new FormCollection(controllerContext.HttpContext.Request.Form) FormCollection is a type we added to ASP.NET MVC that has its own ModelBinder. You can look at the code for FormCollectionBinderAttribute to see what I mean. Accessing the form collection directly appears

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

为君一笑 提交于 2019-11-30 17:16:39
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' string properties) ASP.NET MVC 1 model.Foo = ""; model.Bar = "cat": ASP.NET MVC 2 (preview 1 through RC) model.Foo = null; model.Bar = "cat": Wanted to give anyone who is playing with V2 a heads up since this wasn't mentioned in the ' gu-notes '. Also curious if anyone in the know can comment on whether or not this will be the final implementation or a