Why does the model binder need an empty constructor

前端 未结 4 1650
闹比i
闹比i 2020-12-21 05:21

I need some help with some fundamentals here...

I have this controller that serves up my view with an instance of a class (at least that\'s how I think it works). So

相关标签:
4条回答
  • 2020-12-21 05:25

    Why do I need a blank constructor?

    because of

    [HttpPost] 
    public ActionResult Index(MyModel foo){ ... }
    

    You asked the binder to give you a concrete instance on Post, so the binder needs to create that object for you. Your original object does not persist between the GET and POST actions, only (some of) its properties live on as HTML fields. Thats what "HTTP is stateless" means.

    It becomes a little more obvious when you use the lower level

    [HttpPost] 
    public ActionResult Index(FormCollection collection)
    { 
          var Foo = new MyModel();
          // load the properties from the FormCollection yourself
    }
    

    why would poo.someString change by the time I got to RedirctToAction("End", "EndCntrl", poo)?

    Because someString isn't used in your View. So it will always be blank when you get the new model back. To change that:

    @model myApp.models.MyModel    
    @html.HiddenFor(m => m.SomeString) 
    
    @html.EditorFor(m => m.hi) 
    <input type="submit" value="hit"/>
    

    this will store the value as a hidden field in the HTML and it will be restored for you on POST.

    0 讨论(0)
  • 2020-12-21 05:35

    It doesn't need a parameter less constructor as long you do not define any constructors. If you define a constructor with parameters, you need then a parameter less constructor as it is the one used by the model binder..

    when you postback the values, the binder will map your request in a typed object, it first creates the object, and then tries to map your posted values to some property.

    If you can not have a parameter less constructor... if code is not Under your control, then you have to create a Custom binder.

    0 讨论(0)
  • 2020-12-21 05:47

    I am a little confused by the question but instead have you tried this:

    [HttpPost] 
    public ActionResult Index(MyModel foo){
      if(foo.someString == "laaaa")
        return RedirctToAction("End", "EndCntrl", foo);
      else
        throw new Exception();
    }
    

    You only need a parameterless constructor if you added a parameterized constructor.

    Ex: MyObject item = new MyObject();

    0 讨论(0)
  • 2020-12-21 05:50

    There is no direct connection between model you pass to view and model that you receive in request. In ultimate case the code for initial request and response will run in different instance of IIS or even different machines.

    So when request come back ASP.Net MVC need to recreate all objects (controller, model,...). Having default constructor allows run-time to create new object without knowledge of particular arguments to your custom constructor.

    Side note: Similar reconstruction for constructor exist for generics where you can only specify where T:new() for default constructor.

    0 讨论(0)
提交回复
热议问题