custom-model-binder

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

谁都会走 提交于 2019-11-29 14:08:33
问题 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 receives these values. An MVC model has to be populated from posted form values or query string parameters, or other sources of data. But what mechanism determines the model type handed to the ModelBindingContext, and how is one model type chosen over

ASP.NET Web API Operation with interfaces instead concrete class

可紊 提交于 2019-11-29 08:12:55
My team needs to develop a framework for our company and products using this framework. One of the requisites is that a product could be customized for a specific client, yet it should be easily updated with another version of the same product (not automatically). We're using ASP.NET MVC 4 + Web API (for now, a desktop product will be created next year, based on our framework), NHibernate, heavily IoC using Autofac as DI container and N-Layers. So, in some points of the WebApp, we're using ViewModels as Interfaces with one default implementation and using Autofac to link them, and that's is

Problem with double values binding

喜夏-厌秋 提交于 2019-11-29 06:57:42
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 in complex model objects (currently I work with collections of objects, that contains identifiers and values). What i should use: Value Providers or Model Binders? Please, show code example of solving my problem. You could use a custom model binder: public class DoubleModelBinder : DefaultModelBinder { public override object BindModel

ASP.NET MVC2 - Custom Model Binder Examples

时光怂恿深爱的人放手 提交于 2019-11-28 16:51:32
问题 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 DefaultModelBinder source code to try to get a general feel for what I need to do, but it's entirely more complicated than my scenario and I'm having trouble isolating the specific logic I need to implement. My goal is to take a collection of Checkbox

asp.net core custom model binder just for one property

为君一笑 提交于 2019-11-28 13:33:05
I have a simple model for my asp.net core controller: [HttpPost] public async Task<DefaultResponse> AddCourse([FromBody]CourseDto dto) { var response = await _courseService.AddCourse(dto); return response; } My model is : public class CourseDto { public int Id { get; set; } public string Name { get; set; } public string Genre { get; set; } public string Duration { get; set; } public string Level { get; set; } public string AgeRange { get; set; } public string Notes { get; set; } public bool Active { get; set; } public string OrganisationCode { get; set; } } I'm trying to set value of

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

江枫思渺然 提交于 2019-11-28 07:47:01
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; } The form contains an array or Prices as keys keys: "Prices[0].Value" "Prices[0].ID" "Prices[1].Value" "Prices[1].ID" ... The ViewModel contains a Prices property: public List<Price> Prices { get; set; } The default model binder works nicely as long as the user enters a decimal-convertible string into the Value input. I would like to allow inputs like

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

怎甘沉沦 提交于 2019-11-27 15:37:54
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 need this because I'm getting IEntityChangeTracker problems where I effectively have two repositories accessing the same object graph. So to keep my test project simple, I'm just trying to share a dummy repository. The problem I'm having is that it works on the first request and that's it. The relevant code is below. NInjectModule: public class

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

纵饮孤独 提交于 2019-11-27 10:42:41
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, ModelBindingContext bindingContext) { ValueProviderResult value = bindingContext.ValueProvider.GetValue("ConfirmationMessage"); ValueProviderResult value2 = bindingContext.ValueProvider.GetValue("ConfirmationMessage2"); } You have a few options. On the model add this attribute to each property that you need to allow HTML - best choice using System

ASP.NET MVC Model Binder with Global Number Formats

谁说胖子不能爱 提交于 2019-11-27 06:03:01
问题 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 BaseController. I have tried adding a custom model binder and overriding the bindModel function but I can't see how to get around the error in there as the Culture has already been set back to the default of en-GB. So I tried adding an action

Custom model binder for a property

时光怂恿深爱的人放手 提交于 2019-11-27 04:03:12
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 IList<int> PropertyB {get; set;} } So DefaultModelBinder should bind this without a problem. The only thing is that I want to use special/custom binder for binding PropertyB and I also want to reuse this binder. So I thought that solution would be to put a ModelBinder attribute before the PropertyB which of course doesn't work (ModelBinder attribute is not allowed on a