0

I would like to have in the registration form in my site an interface. lets call it IShape. there are serval interface implementations, like ISquare and ICycle, each one has its own properties except the shared ones declared in the interface.

I want the user to have the ability to have as many shapes as he wants from all types posssible, and be able to fill them with information, which I receive as RegisterModel.

public class RegisterModel
{
    [Required]
    [Display(Name = "Username")]
    public string UserName { get; set; }

    [Required]
    [Display(Name = "Shapes")]
    public List<string> Shapes { get; set; }
}

Register Action:

public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
              // Iterate over the shapes
              model.Shapes ...
            }
        }

Please refer to the javascript issue, since if I have multiple objects their names will conflict. For example: 2 shapes has size property. if I create two textboxes with the name "size", The ASP.NET engine won't be able to solve it.

Thanks!

3 Answers 3

1

I would look at using Knockout.js for the front end to allow the user to construct their list of objects. It is an MVVM framework that lets you manage client side events and manipulate the DOM.

You could send an ajax request back to the server for each new shape they add (on the onclick event). You would need to then persist the list the user is building up in the database or session, but when the user hits confirm/submit you would already have access to the full list so it will be easy to perform validation on all the items at once.

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

1 Comment

I explored a bit and saw this page: knockoutjs.com/examples/contactsEditor.html Which is what I am looking for (It needs some customization ofcouse). It will generate me a json object, that after validation will be sent to a API controller or Web API. My API will recognize the interface using reflection to get all the classes in my dll that inherit from my interface, and try matching the Jason keys to the class's properties (Using JSON.NET). everything is planned, just need to code that :)
1

How exactly would you expect the MVC framework to model bind an interface? In order for model binding to work, MVC must know what instance of an object it has to create. There isn't enough information in an interface to do that, which will mean you will need custom model binders.

You're talking about going down a path that essentially overrides all the productivity features that MVC gives you out of the box, because most MVC features are based on convention over configuration. It's hard to have conventions with interfaces.

I'm not saying you can't do it, but there is probably a much better way to go about it.

1 Comment

You are absolutely right about that I shouldn't interrupt the basics of the MVC framework. I thought of a friendly solution in my opinion written as a comment to Nick's answer. Thanks
0

I think I kind of understand and it is quite reasonable but using Interfaces is not the way to go about it because an Interface can never be instantiated, only a class that implements it can.

What I think you need to do is use inheritance rather than interfaces.

You have a base abstract Shape class of which Circle, Square etc derive from. On the input form your user can happily select Circles or Squares and then you get a nice unified IList of type Shape to process in your controller.

In order to do this you would need to write a custom model binder that detects that it is trying to bind an abstract class (which it cannot do) and overrides CreateModel to provide a new class of type Square or Circle (which is then the class that MVC binds to).

There is a fairly full and detailed discussion of this technique here

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.