ASP.NET-MVC2 Preview 1: Are There Any Breaking Changes?

后端 未结 4 474
长情又很酷
长情又很酷 2020-12-12 07:23
  1. I was following Steven Sanderson\'s \'Pro ASP.NET MVC Framework\' book.
  2. On page 132, in accordance with the author\'s recommendation, I downloaded the ASP.NET
相关标签:
4条回答
  • 2020-12-12 07:42

    Another change is when dealing with form lists from your ViewModel.

    For example in MVC 1.0 if you had a list of objects IList< MyObject > displayed in your View

    <% for (int i = 0; i < Model.Length; i++) { %>
        <%= Html.TextBox("MyObject[" + i + "].FirstName") %>
        <%= Html.TextBox("MyObject[" + i + "].LastName") %>
    <% } %>
    

    The input boxes would be rendered as

    <input id="MyObject[0]_FirstName" name="MyObject[0].FirstName" type="text" value="" />
    

    Note: id = MyObject[0]_FirstName and name = MyObject[0].FirstName

    However, in MVC 2.0 they are rendered as

    <input id="MyObject_0__FirstName" name="MyObject[0].FirstName" type="text" value="" />
    

    Note: id = MyObject_[0]__FirstName and name = MyObject[0].FirstName

    This broke some jquery I was using to manipulate my table data. Notice the single and double underscores in the id.

    0 讨论(0)
  • 2020-12-12 07:50

    If you are working from Steven Sanderson's book, Pro ASP.Net MVC Framework, then you'll need to make a change to the WindsorControllerFactory class to work with the interface exposed by the DefaultControllerFactory. This class was updated in the MVC 2 Preview 2. Who knows what the final release will hold.

    Change:

    protected override IController GetControllerInstance(Type controllerType)
    

    To:

    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    

    I've also found it necessary to ensure that the MVC 2 Preview 2 framework hasn't passed in a non-controller class (it was unexpectedly passing in favicon.ico as the controllerType). So the updated GetControllerInstance method looks like this:

    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
       //Debug.Print("Controller: {0}, Action: {1}", requestContext.RouteData.Values["Controller"], requestContext.RouteData.Values["Action"]);
    
       if (controllerType == null)
       {
          //Debug.Print("Is IController: {0}", (controllerType is IController));
          //Debug.Print("Is null: {0}", (controllerType == null));
          return null;
       } 
          else 
       {
          return (IController)container.Resolve(controllerType);
       }
    }
    

    I left the Debug code in there in case you're interested in seeing why you're getting a "Value cannot be null" error in the WindsorControllerFactory.

    After making this change, Windsor began working for me. Hopefully, this will help.

    Cheers, Jason

    0 讨论(0)
  • 2020-12-12 07:56

    I had problems with Castle and MVC2 in VS 2010 Beta 2.

    I was able to get Castle working with this code for the GetControllerInstance. The problem was that all requests were coming into this (like css files), so just returning null for those seems to work.

    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
         if (controllerType == null) { return null; }
    
         return (IController)_container.Resolve(controllerType);
    }
    

    -Damien

    0 讨论(0)
  • 2020-12-12 08:05

    From ScottGu's blog (unanswered as of this moment):

    Tuesday, August 18, 2009 1:36 PM by Patrick Cooper Scott,

    Love the direction of MVC. One question, in MVC 2, you've changed the signature for GetControllerInstance from just System.Type to System.Web.Routing.RequestContext and System.Type. Is intentional and permanent? It's causing problems with Castle that I'm not sure yet how to handle.

    Thanks!

    Patrick

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