custom-model-binder

Error implementing a Custom Model Binder in Asp.Net Web API

流过昼夜 提交于 2019-12-06 01:00:03
im stuck with this very strange problem. i have an API Controller named AttendanceController derived from APIControllerFA which is inturn derived from ApiController here is the code public class AttendanceController : ApiControllerFA { public HttpResponseMessage PostAttendance([ModelBinder(typeof(AttendanceRegistrationModelBinder))]AttendanceRegistrationModel model) { //checking model state for errors //throw new Exception("Just to throw an error "); ........... As can be seen on the PostAttendance method i have a custom ModelBinder named AttendenceRegistrationModelBinder for which this is the

.NET core custom and default binding combined

柔情痞子 提交于 2019-12-05 05:59:37
I'm creating a custom model binder for a view model, implementing IModelBinder I have a lot of properties in my view model, the majority of which do not need any custom binding. Rather than explicitly set all of the property values on my model individually from the ModelBindingContext , I would to be able to get the framework to bind the model for me, then I would carry out any custom binding: public class ApplicationViewModelBinder : IModelBinder { public Task BindModelAsync(ModelBindingContext bindingContext) { if (bindingContext == null) { throw new ArgumentNullException(nameof

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

老子叫甜甜 提交于 2019-12-03 22:13:37
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 { public string Search { get; set; } public int Max { get; set; } } I'm requesting it with: http:/

Post a List<Interface> .net core 1.0

被刻印的时光 ゝ 提交于 2019-12-03 16:13:51
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 because of the List of Interfaces. I've been looking on how to implement a custom model binder, and seen

Custom Model Binder Not Validating Model

半世苍凉 提交于 2019-12-03 12:01:23
I started to play around with knockout.js and in doing so I used the FromJsonAttribute (created by Steve Sanderson). I ran into an issue with the custom attribute not performing model validation. I put together a simple example-- I know it looks like a lot of code-- but the basic issue is how to force the validation of the model within a custom model binder. using System.ComponentModel.DataAnnotations; namespace BindingExamples.Models { public class Widget { [Required] public string Name { get; set; } } } and here is my controller: using System; using System.Web.Mvc; using BindingExamples

WebApi2: Custom parameter binding to bind partial parameters

泪湿孤枕 提交于 2019-12-03 07:52:30
问题 I have a webApi2 project and an other project, in which I have my Model classes and a BaseModel that is a base for all Models, as following, public class BaseModel { public string UserId { get; set; } } All the other models are derived from my BaseModel. In webapi I have my CustomerController as following, public class CustomerController : ApiController { [HttpPost] public GetCustomerResponseModel Get(GetCustomerRequestModel requestModel) { var response = new GetCustomerResponseModel(); //I

ModelState.IsValid is false prior to validation

随声附和 提交于 2019-12-02 11:24:18
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); } And this works pretty well, here's the override part of the custom model binder : protected override

ASP.NET MVC routing issue with Google Chrome client

假装没事ソ 提交于 2019-12-01 12:54:43
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.Content = response.ToString(); return result; } If I uncomment [OutputCache] attribute then it works

Validating a view model after custom model binding

只愿长相守 提交于 2019-11-30 19:04:24
问题 I have a view model that implements IValidatableObject that contains a string and a collection of another view model, something like this: public sealed class MainViewModel { public string Name { get; set; } public ICollection<OtherViewModel> Others { get; set; } } My validation checks each object in Others against different rules using the contract provided by IValidatableObject : public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) { foreach (var other in this

Custom Model Binder for ASP.NET MVC on GET request

混江龙づ霸主 提交于 2019-11-29 16:41:00
问题 I've created a custom MVC Model Binder which gets called for every HttpPost that comes into the server. But does not get called for HttpGet requests. Should my custom model binder get called during a GET ? If so, what did I miss? If not, How can I write custom code handling the QueryString from a GET Request? Here's my implementation... public class CustomModelBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext