custom-model-binder

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

∥☆過路亽.° 提交于 2019-11-27 02:03:54
问题 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

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

試著忘記壹切 提交于 2019-11-26 11:58:06
问题 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\"); } 回答1: You have a few

Custom model binder for a property

断了今生、忘了曾经 提交于 2019-11-26 10:57:54
问题 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

Custom DateTime model binder in Asp.net MVC

允我心安 提交于 2019-11-26 10:31:33
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 binder part is a bit more difficult. I would like to add a new model binder for type DateTime . I can either implement IModelBinder interface and write my own BindModel() method inherit from DefaultModelBinder and override BindModel() method My model has a property as seen above ( Birth ). So when the model tries to bind request data to this property

Custom DateTime model binder in Asp.net MVC

家住魔仙堡 提交于 2019-11-26 03:28:22
问题 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 binder part is a bit more difficult. I would like to add a new model binder for type DateTime . I can either implement IModelBinder interface and write my own BindModel() method inherit from DefaultModelBinder and override BindModel() method My