1

This is a general model binding question that applies to MVC 1 & 2. I'm wondering if MVC2 would be better for this, but here's my question:

I have a fairly complex model:

public interface IEvent
  public int Id
  public string Title
  public List<EventContact> Contacts
  public List<EventDatesLocations> DatesLocations

public class EventDatesLocations
  public int Id
  public DateTime StartDate
  public DateTime EndDate
  public List<EventLocation> Locations

I have a custom model binder for my IEvent class that does basically all the binding for IEvent. I call the default model binder for binding the Contacts list, which works well.

I am ready to start trying to bind the DatesLocations stuff, but I want to make sure I'm doing things correctly.

Overall, I'm not sure I have all the details on model binding understood. Would it be better to have multiple model binders for the lists within IEvent or just have one IEvent model binder (as I'm doing now) which calls the default binder for the lists it needs.

How do the experts do it? :P

2
  • 1
    I'm no expert at model binding, however I don't think there is anything here that would require a custom model binder. How are you binding to you lists? Here is an article on how the default binder handles them. hanselman.com/blog/… Commented Mar 24, 2010 at 15:18
  • @Ryan - The main motivator for binding using a custom model binder was the fact that I'm using an interface, IEvent as my main event object. Using the default model binder, it immediately told me it could not create an instance of IEvent. That, and I need to bind specific properties based on what type of IEvent it is. I use the default model binder with a paired-down valueprovider dictionary Commented Mar 24, 2010 at 19:58

1 Answer 1

0

Take a look at this question to see some nested model binding.

Back to this question, you mention you need to do some binding on different implementations of IEvent, but then you want to bind specific properties depending on what type implements IEvent. I would say this is generally bad. Why? Because you are trying to get one model binder knowing about an Interface knowing about specific implementations of it.

If you require multiple classes to implement IEvent (for some reason) then you will probably need custom binders for each (if going down this route, although as they will be concrete types then the default binder should work), or you need to expand the IEvent interface to include the properties you will be setting on all.

Remember Interfaces are there to define a contract that needs to be implemented ... you can't work with objects which implement it and then check for other properties depending on what type they are :-)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.