custom-model-binder

How to implement WebApi custom model binder

旧城冷巷雨未停 提交于 2019-12-13 19:41:56
问题 I am implementing custom model binder. I researched how to do it and found out that in case of WebApi I need to implement IModelBinder interface. My implementation looks like this: public class ModelBaseBinder : IModelBinder { public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext) { if ((bindingContext.Model is MyModel)) { //my code here controller.InitModel(model); return true; } return false; } } I am just not sure if my implenentaton is complete. Do I

mvc6 custom model binder not firing

前提是你 提交于 2019-12-12 15:12:10
问题 I'm trying to figure out model binding in current mvc6 (visual studio 2015 release candidate). This is what my code looks like so far: public class MyObjectModelBinder : IModelBinder { public Task<ModelBindingResult> BindModelAsync(ModelBindingContext bindingContext) { if (bindingContext.ModelType == typeof(MyObject)) { var model = new MyObject(); return Task.FromResult(new ModelBindingResult(model, bindingContext.ModelName, true)); } return Task.FromResult<ModelBindingResult>(null); } } The

KendoUI grid does not fire saveChanges event ( System.MissingMethodException: Cannot create an instance of an interface.)

折月煮酒 提交于 2019-12-12 02:57:21
问题 KendoUI grid does not fire SaveChanges event. I did not see any problem in Environment of Visual Studio 2013 and browsers(FF, IE, Chrome) when I run the project and clicked on SaveChanges button but SaveChanges event does not fire. For further investigation, I've used debugger of Chrome and I saw a problem. Please see the pictures of problem, error log and codes. (I've used JQuery v1.10) Please help me to solve it. EDITED: I understood that error has related with [Bind(Prefix = "models")]

ASP .NET Web API ModelBinder single parameter

蓝咒 提交于 2019-12-11 15:42:54
问题 Currently I've got this ModelBinder that works just fine: public class FooModelBinder : IModelBinder { public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext) { var body = JObject.Parse(actionContext.Request.Content.ReadAsStringAsync().Result); IEnumerable<Foo1> media = new List<Foo1>(); var transaction = body.ToObject<Foo2>(); media = body["Media"].ToObject<List<Foo3>>(); transaction.Media = media; bindingContext.Model = transaction; return true; } } As you

How to write unit test for custom model binder in ASP.net core

♀尐吖头ヾ 提交于 2019-12-11 02:48:41
问题 I have written custom model binder for a property. Now I am trying to write unit test for the same but not able to create object for model binder. Can anyone help me ? Below is the code for which I have to write test. public class JourneyTypesModelBinder : IModelBinder { public Task BindModelAsync(ModelBindingContext bindingContext) { bool IsSingleWay = Convert.ToBoolean((bindingContext.ValueProvider.GetValue("IsSingleWay")).FirstValue); bool IsMultiWay = Convert.ToBoolean((bindingContext

Bind Exclude not working Model Binding for child objects in ASP.Net MVC

坚强是说给别人听的谎言 提交于 2019-12-11 02:44:30
问题 I got into a issue in Model Binding in Asp.Net MVC. I have view model like below, public class ArticleViewModel : BaseViewModel { public Article art { get; set; } public List<ArticleAttachment> attachments { get; set; } } I am trying to exclude model binding a property on the "Article" child object as seen below in my action method, [HttpPost] [ValidateInput(false)] public ActionResult New([Bind(Exclude = "art.Abstract")]ArticleViewModel articleVM) { But the model binder populates the

How can I build a custom model binder which will return different types of models depending on the request context?

不羁的心 提交于 2019-12-10 18:35:29
问题 I have incoming requests (from Facebook for Credits handling) on a specific action which will have have different contents, so I have different model classes to handle that. This is my action: public ActionResult Index([ModelBinder(typeof(FacebookCreditModelBinder))] IFacebookRequest facebookRequest) { if (facebookRequest is FacebookPaymentsGetItemsRequest) { // do whatever } } And this is my model binder. public class FacebookCreditModelBinder : DefaultModelBinder { public override object

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

跟風遠走 提交于 2019-12-07 16:31:16
问题 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

Custom DateTime model binder in ASP.NET Core 1 (RTM)

流过昼夜 提交于 2019-12-07 11:30:00
问题 I'm writing and ASP.NET Core 1 application, which has a Web API Controller where supplier will post data. This is a simplified version of the Model class to which I will bind the incoming data: public class Lead { public int SupplierId { get; set; } public DateTime Date { get; set; } } The problem is that the dates will be posted with German formatting, for instance, 30.09.2016 . I do not want to set the global culture of the application to de-DE because a) I'm not German, and b) the rest of

.NET core custom and default binding combined

主宰稳场 提交于 2019-12-07 04:00:48
问题 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