aspnet / Mvc

[Archived] ASP.NET Core MVC is a model view controller framework for building dynamic web sites with clean separation of concerns, including the merged MVC, Web API, and Web Pages w/ Razor. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
5.61k stars 2.14k forks source link

model binding with recursive interface in rc2 #4819

Closed rontran closed 8 years ago

rontran commented 8 years ago

So I have a viewmodels

 public class SurveyViewModel
    {
        public SurveyViewModel() {
            Controls = new List<IControl>();
        }
        public IList<IControl> Controls{ get; set; }     
    }

   public class SectionViewModel :  IControl {
        public SectionViewModel() {
            Controls = new List<IControl>();
        }
        public IList<IControl> Controls { get; set; }
        public string Label { get; set; }
     }

   public QuestionViewModel :IControl
    {
        public string Label { get; set; }
    }

Basically I have a survey with many controls that can be a section(with many controls), question, a label, a button etc. I am using a displaytemplate to loop through the collections. It works but however when I postback, I, of course, get the "Cannot create an instance of an interface.". How do I intercept the modal binding. In old version, we had CreateModel so I can return a List<IControl> or the right object based on a hidden input.

I used the answer provided in https://github.com/aspnet/Mvc/issues/4703 and I still get the same error.

dougbu commented 8 years ago

@rontran we probably need a small GitHub repo. It's likely the problem is either a detail that's important in your case or something from #4703 you didn't implement.

rontran commented 8 years ago

I've uploaded my POC at https://github.com/rontran/ModelBinding

Thanks!

dougbu commented 8 years ago

The repro project contained a number of bugs. I opened rontran/ModelBinding#1 with a few fixes. I'll also check #4703 to see if any recommendations are misleading.

I didn't see "Cannot create an instance of an interface." errors but the model binder provider would overflow the stack.

dougbu commented 8 years ago

@rontran please close this issue if I've answered your questions.

rontran commented 8 years ago

It works. I had problem compiling your changes but after updating to update 3, it works. Thanks alot!