custom-model-binder

Custom Model Binder for DropDownList not Selecting Correct Value

旧巷老猫 提交于 2020-01-24 19:33:09
问题 i've created my own custom model binder to handle a Section DropDownList defined in my view as: Html.DropDownListFor(m => m.Category.Section, new SelectList(Model.Sections, "SectionID", "SectionName"), "-- Please Select --") And here is my model binder: public class SectionModelBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); if

Post a List<Interface> .net core 1.0

本秂侑毒 提交于 2020-01-12 08:58:29
问题 I'm building a dynamic form creator in .net core. A "form" will consist of many different form elements. So the form model will look something like this: public class FormModel { public string FormName {get;set;} public List<IElements> Elements{get;set;} } I have classes for TextBoxElement , TextAreaElement , CheckBoxElement that all implement the IElemets interface. And I have EditorTemplates for each element. The code to render the form works great. Though posting the form does not work

dot net core custom model binding for generic type parameters in mvc action methods

ⅰ亾dé卋堺 提交于 2020-01-05 05:51:53
问题 I am building a simple search, sort, page feature. I have attached the code below. Below are the usecases: My goal is to pass the "current filters" via each request to persist them particularly while sorting and paging. Instead of polluting my action method with many (if not too many) parameters, I am thinking to use a generic type parameter that holds the current filters. I need a custom model binder that can be able to achieve this. Could someone please post an example implementation? PS: I

MVC Date Time Model Binding

本小妞迷上赌 提交于 2020-01-04 02:46:12
问题 I am using 2 kendo date pickers in my application as such: <div class="span12"> <div class="span2" style="text-align: right"> Start Date: </div> <div class="span2"> @(Html.Kendo().DatePickerFor(m=>m.StartDate)) </div> <div class="span2" style="text-align: right"> End Date: </div> <div class="span2"> @(Html.Kendo().DatePickerFor(m=>m.EndDate)) </div> <div class="span4"> <button class="btn btn-primary" onclick="getGraphData()">Show</button> </div> </div> When the button is clicked, I read the

Custom model binder not called when type is nullable

筅森魡賤 提交于 2020-01-03 08:58:07
问题 I have a custom struct called TimeOfDay which is used in a view model like this: public class MyViewModel { public TimeOfDay TimeOfDay { get; set; } } I have created a custom model binder called TimeOfDayModelBinder and registered it in Global.asax.cs like this: ModelBinders.Binders.Add(typeof(TimeOfDay), new TimeOfDayModelBinder()); And everything works great. However, if I change my view model to this: public class MyViewModel { public TimeOfDay? TimeOfDay { get; set; } // Now nullable! }

ASP.Net Web API model binding not working like it does in MVC 3

£可爱£侵袭症+ 提交于 2020-01-01 07:30:22
问题 I was under the impression that model binding in the ASP.Net Web API was supposed to support binding with the same minimum level of functionality supported by MVC. Take the following controller: public class WordsController : ApiController { private string[] _words = new [] { "apple", "ball", "cat", "dog" }; public IEnumerable<string> Get(SearchModel searchSearchModel) { return _words .Where(w => w.Contains(searchSearchModel.Search)) .Take(searchSearchModel.Max); } } public class SearchModel

ModelState.IsValid is false prior to validation

怎甘沉沦 提交于 2019-12-31 06:58:40
问题 We wrote a custom model binder that overrides the CreateModel method of the ComplexTypeModelBinder so that we can have injection into our ViewModels instead of having to pass the injected clients and repos to our model from the controller . For example, for a model like this: public class ThingViewModel { public ThingViewModel (IThingRepo thingRepo) {} } In our controller we can do: public class ThingController : Controller { public IActionResult Index(ThingViewModel model) => View(model); }

ASP.NET MVC routing issue with Google Chrome client

怎甘沉沦 提交于 2019-12-30 11:34:33
问题 My Silverlight 4 app is hosted in ASP.NET MVC 2 web application. It works fine when I browse with Internet Explorer 8. However Google Chrome (version 5) cannot find ASP.NET controllers. Specifically, the following ASP.NET controller works both with Chrome and IE. //[OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")] public ContentResult TestMe() { ContentResult result = new ContentResult(); XElement response = new XElement("SvrResponse", new XElement("Data", "my data")); result

Change culture before ModelBinder is used

拥有回忆 提交于 2019-12-30 03:07:24
问题 I want to create a website in different languages. I already read that I could create an ActionFilter, but I have a litte problem: I had to create a custom ModelBinder in order to work with english and german number formats ( 123,456,789.1 vs. 123.456.789,1 ) public class DecimalModelBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { string key = bindingContext.ModelName; var v = ((string[])bindingContext

Problem with double values binding

微笑、不失礼 提交于 2019-12-29 06:54:36
问题 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. 回答1: You could use a custom model