modelbinders

Asp.Net MVC 2 - Iterate Through Form Values In Model Binder

I have a list of items in my form which are named like this... <input type="text" id="ListItem1" name="ListItem1"> <input type="text" id="ListItem2" name="ListItem2"> <input type="text" id="ListItem3" name="ListItem3"> I want to create a custom model binder which converts these in to model with this structure... public class MyModel ...

ASP.NET MVC - Custom object instantiation / providing with the Modelbinder?

Is it possible to customize the object initiation / providing with the default ASP.NET MVC Modelbinder? <AcceptVerbs(HttpVerbs.Post)> _ Function EditObject(id As int, <Bind(Exclude:="Id")> obj As BLL.Object) As ActionResult End Function I would like to 'provide' obj to the modelbinder. (i don't want the Modelbinder to use New() and i...

ASP.NET MVC 2: Updating a Linq-To-Sql Entity with an EntitySet

I have a Linq to Sql Entity which has an EntitySet. In my View I display the Entity with it's properties plus an editable list for the child entites. The user can dynamically add and delete those child entities. The DefaultModelBinder works fine so far, it correctly binds the child entites. Now my problem is that I just can't get Linq T...

Custom Model binder not firing

This is my custom model binder. I have my breakpoint set at BindModel but does not get fired with this controller action: public ActionResult Create(TabGroup tabGroup) ... public class BaseContentObjectCommonPropertiesBinder : DefaultModelBinder { public new object BindModel(ControllerContext controllerContext, ModelBindingContex...

Add custom Model Binders for derived objects and their base

My class diagram: BaseContentClass Page inherits BaseContentClass Tab inherits BaseContentClass ... If I do this ModelBinders.Binders.Add(typeof(BaseContentObject), new BaseContentObjectCommonPropertiesBinder()); then when in controller action parameter of type Tab appears, custom model binder is not fired. It gets fired if I d...

Setting ModelState values in custom model binder

I am using custom model binder in ASP.NET MVC 2 that looks like this: public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { if (controllerContext == null) { throw new ArgumentNullException("controllerContext"); } if (bindingContext ...

What is the proper way to access datastore in custom Model Binders?

How should I properly implement data access in my custom model binders? Like in controllers I use IContentRepository and then have it create an instance of its implementing class in constructor. So I have everything ready for incorporating IoC (DI) at a later stage. Now I need something similar in model binder. I need to make some DB ...

ModelBinders, complex nested Types and Interfaces

Hi, I've a scenario where I need to bind to an interface - in order to create the correct type, I've got a custom model binder that knows how to create the correct concrete type (which can differ). However, the type created never has the fields correctly filled in. I know I'm missing something blindingly simple here, but can anyone tel...

ValueProvider.GetValue Extension Method

I have a model like this; public class QuickQuote { [Required] public Enumerations.AUSTRALIA_STATES state { get; set; } [Required] public Enumerations.FAMILY_TYPE familyType { get; set; } As you can see the two proerties are enumerations. Now I want to employ my own model binder for reasons that I won't bother getti...

Is it okay to hit the database from a custom model binder?

Say I have an object that gets some data from HttpPost and some from the database. I think I want to allow the ModelBinder to go to the database/repository for the that data missing from the post. In practice, is this a good or bad idea? ...

NullReferenceException when testing DefaultModelBinder.

I'm developing a project using BDD/TDD techniques and I'm trying my best to stay the course. A problem I just ran into is unit testing the DefaultModelBinder. I'm using mspec to write my tests. I have a class like this that I want to bind to: public class EmailMessageInput : IMessageInput { public object Recipient ...

ASP.Net MVC elegant UI and ModelBinder authorization

We know that authorization's stuff is a cross cutting concern, and we do anything we could to avoid merge business logic in our views. But I still not find an elegant way to filter UI components (e.g. widgets, form elements, tables, etc) using the current user roles without contaminate the view with business logic. same applies for mode...

ASP.NET MVC 2: Linq to SQL entity w/ ForeignKey relationship and Default ModelBinder strangeness

Once again I'm having trouble with Linq to Sql and the MVC Model Binder. I have Linq to Sql generated classes, to illustrate them they look similar to this: public class Client { public int ClientID { get; set; } public string Name { get; set; } } public class Site { public int SiteID { get; set; } public string Name {...

ASP.NET MVC 2 - Is it worth creating a custom model binder for this scenario?

Well i have a complex form view model like this : public class TransactionFormViewModel { public Session SessionRecord { get; private set; } public IEnumerable<Resource> ResourcePerSessionRecord { get; private set; } public Person PersonRecord { get; private set; } public decimal SubTotal { get; private set; } publi...

ASP.Net MVC2 CustomModelBinder not working... Changed from MVC1

(My apologies if this seems verbose - trying to provide all relevant code) I've just upgraded to VS2010, and am now having trouble trying to get a new CustomModelBinder working. In MVC1 I would have written something like public class AwardModelBinder: DefaultModelBinder { : public override object BindModel(ControllerContext c...

Custom BindAttribute

Is there a way to implement your own BindAttribute. Preferably by deriving from BindAttribute, but if you need to impliment it in your own ModelBinder how would you go about doing this? ...

breaking datetime into constituent parts ASP.NET MVC form

hi guys, i have searched the web relentlessly for this and have not found anything - which is surprising because i would think it is such a common scenario! Basically, on my model i have a DateTime field which i wish the user to populate through a form. I am using the Html helper to render all other parts of the form (along with valida...

How to use NInject (or other DI / IoC container) with the model binder in ASP.NET MVC 2 ?

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 = timeProv...

ASP.Net MVC 2 - better ModelBinding for Dictionary<int, int>

Hi, In some special cases you will need a list of textboxes (to deal with n - n associations) whose id is not know before runtime. Something like this : http://screencast.com/t/YjIxNjUyNmU In that particular sample I'm looking to associate a count to some of my 'templates'. in ASP.Net MVC 1 I coded a Dictionary ModelBinder to have a c...

Model binding & derived model classes

Does ASP.NET MVC offer any simple way to get model binding to work when you have model classes that inherit from others? In my scenario I have a View that is strongly typed to List<Person>. I have a couple of classes that inherit from Person, namely PersonTypeOne and PersonTypeTwo. I have three strongly typed partial views with names ...