custom-model-binder

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

落花浮王杯 提交于 2019-12-28 04:25:28
问题 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

C# ASP.NET Core ModelBinder not Updating Model

☆樱花仙子☆ 提交于 2019-12-24 07:58:34
问题 I have created a ModelBinder, which only gets triggered if an object has a [Decimal] attribute assigned, yet for some reason, despite it actually sanitising the data it does not seem to update the posted model. I wonder if someone could see from my code below, where I maybe going wrong. Startup.cs public void ConfigureServices(IServiceCollection serviceCollection) { serviceCollection.AddMvc(config => config.ModelBinderProviders.Insert(0, new DecimalModelBinderProvider())); }

An entity object cannot be referenced by multiple instances of IEntityChangeTracker error

与世无争的帅哥 提交于 2019-12-24 07:29:15
问题 I understand there are a lot of duplicates to this question, but I couldn't find one that fits my scenario. So I am using the ASP.NET MVC 4 + Entity Framework + Ninject using repository pattern (I see many mentions of repository + unit of work pattern? That could be a potential fix to my problem but I don't know how to implement it). When I try to add a new post, I get "An entity object cannot be referenced by multiple instances of IEntityChangeTracker" error on the following line of code

ASP.NET MVC - Custom Model Binder for ID fields

与世无争的帅哥 提交于 2019-12-24 00:44:39
问题 i have the following entities: public class Category { public virtual int CategoryID { get; set; } [Required(ErrorMessage = "Section is required")] public virtual Section Section { get; set; } [Required(ErrorMessage = "Category Name is required")] public virtual string CategoryName { get; set; } } public class Section { public virtual int SectionID { get; set; } public virtual string SectionName { get; set; } } Now within my add category view i have a textbox to enter the SectionID eg: <%=

How to call Default Model Binding from custom binder in WebAPI?

安稳与你 提交于 2019-12-23 12:34:14
问题 I have a custom model binder in WebAPI that uses the following method from the `Sytem.Web.Http.ModelBinding' namespace which is the correct namespace for creating custom model binders for Web API: public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext) { } I have a HTTP POST on a controller for which I want to use this custom model binder. The posted object contains roughly 100 fields. I want to change 2 of them. What I need is for default model binding to

Model availability inside ActionFilter

自作多情 提交于 2019-12-23 04:28:42
问题 I have created a new ActionFilter for an ASP.NET MVC application that I'm creating. I have an action which accepts an Http Post and the argument of the action method accepts an object, for which I have created and registered a custom model binder. I noticed that inside the IActionFilter.OnActionExecuting the value for filterContext.Controller.ViewData.Model is always null despite the fact that it looks like the model binder is always invoked before the action filter OnActionExecuting method.

Custom Model Binder Not Validating Model

房东的猫 提交于 2019-12-21 04:08:57
问题 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 {

ASP.NET Web API Operation with interfaces instead concrete class

那年仲夏 提交于 2019-12-18 05:18:16
问题 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

asp.net core custom model binder just for one property

笑着哭i 提交于 2019-12-17 20:44:30
问题 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

How to use a DI / IoC container with the model binder in ASP.NET MVC 2+?

南笙酒味 提交于 2019-12-17 09:55:52
问题 Let's say I have an User entity and I would want to set it's CreationTime property in the constructor to DateTime.Now. But being a unit test adopter I don't want to access DateTime.Now directly but use an ITimeProvider : public class User { public User(ITimeProvider timeProvider) { // ... this.CreationTime = timeProvider.Now; } // ..... } public interface ITimeProvider { public DateTime Now { get; } } public class TimeProvider : ITimeProvider { public DateTime Now { get { return DateTime.Now;