Ajax with ASP.NET MVC Framework Preview 2

This is the updated version of the Nikhil’s excellent example on how to use Ajax with ASP.net MVC.

I commented all the changes made in the code and prefixed them with //emad. So just search for //emad and you will see all the changes I made… Here is a quick list of the changes I made to make this compile and run on MVC Preview 2:

  1. Removed the [ControllerAction] tags – they are no longer needed.
  2. Updated the Route Table creation in the global.asax.cs file to work with the new format
  3. Updated the Ajax framework project to use the new classes
    1. HttpContextBase instead of IHttpContext
    2. HttpRequestBased instead of IHttpRequest
    3. HttpResponseBase instead of IHttpResponse
  4. Updated the AjaxViewContext constructor to match ViewContext constructor
  5. Changed the web.config to work with the new version (copied and pasted from another preview 2 project)

Note: Test project was not updated and will not compile – sorry, I didn’t have time to do it.

Here is the updated code: tasklist.zip

Advertisements

0 thoughts on “Ajax with ASP.NET MVC Framework Preview 2

  1. Thanks for the patch. Just noticed something a bit strange. In the AjaxViewExtensions class your change results in a recursive call to the calling View (if that makes sense).Thus if you try the following (List.aspx):this.RenderPartial("UserList", ViewData.Users); where UserList is a UserControl (ascx), the RenderPartial calls List.aspx again.To fix this I changed the RenderPartialCore method to: private static void RenderPartialCore(ViewContext viewContext, string viewName, object viewData) { if (String.IsNullOrEmpty(viewName)) throw new ArgumentNullException(viewName); // TODO: Need to be able to get the view engine associated with this controller // without the cast to the specific implementation Controller controller = (Controller)viewContext.Controller; // create a new view context for the view called, not the existing view ViewContext newContext = new ViewContext(controller.ControllerContext, viewName, null, viewData, controller.TempData); //emad ibrahim: controller has a viewengine already controller.ViewEngine.RenderView(newContext); //IViewFactory viewFactory = controller.ViewFactory; //IView partialView = viewFactory.CreateView(viewContext, viewName, null, viewData); //partialView.RenderView(viewContext); }

    Like

  2. Thanks for the patch. Just noticed something a bit strange. In the AjaxViewExtensions class your change results in a recursive call to the calling View (if that makes sense).

    Thus if you try the following (List.aspx):

    this.RenderPartial("UserList", ViewData.Users);

    where UserList is a UserControl (ascx), the RenderPartial calls List.aspx again.

    To fix this I changed the RenderPartialCore method to:

    private static void RenderPartialCore(ViewContext viewContext, string viewName, object viewData)
    {
    if (String.IsNullOrEmpty(viewName))
    throw new ArgumentNullException(viewName);

    // TODO: Need to be able to get the view engine associated with this controller
    // without the cast to the specific implementation
    Controller controller = (Controller)viewContext.Controller;

    // create a new view context for the view called, not the existing view
    ViewContext newContext = new ViewContext(controller.ControllerContext, viewName, null, viewData, controller.TempData);

    //emad ibrahim: controller has a viewengine already
    controller.ViewEngine.RenderView(newContext);
    //IViewFactory viewFactory = controller.ViewFactory;
    //IView partialView = viewFactory.CreateView(viewContext, viewName, null, viewData);
    //partialView.RenderView(viewContext);
    }

    Like

  3. Thanks for the update, also thank to Pieter, but I still have problems with the RenderPartialCore method, like Pieter says when calling the RenderPartial method on a UserControl it becomes a recursive call and we get an overflow exception, after having chnged the methos to Pieters version that doesent happen anymore, however somehow my Viewdata get corrupted so that even though it looks the same when examining it in debud I cannot access the data i the ViewData. eg. calling the ViewData.ContainsDataItem(MyKey) returns false even though examing the object clearly show the existence of the Key and Data.Any thoughts? Updates? I am using preview 2 the interim release. Please answer quickly I am so stuck…

    Like

  4. @Pieter: thanks for sharing your findings@Christian: I am not sure what is going on here. I personally never used ContainsDataItem method on ViewData because I always used a typed ViewData.I have a BaseViewData that is used on the master page and every page using that master and then I create derived classes for ViewData for each page e.g. UserInfoViewData, etc…

    Like

  5. Thanks for the update, also thank to Pieter, but I still have problems with the RenderPartialCore method, like Pieter says when calling the RenderPartial method on a UserControl it becomes a recursive call and we get an overflow exception, after having chnged the methos to Pieters version that doesent happen anymore, however somehow my Viewdata get corrupted so that even though it looks the same when examining it in debud I cannot access the data i the ViewData. eg. calling the ViewData.ContainsDataItem(MyKey) returns false even though examing the object clearly show the existence of the Key and Data.

    Any thoughts? Updates? I am using preview 2 the interim release. Please answer quickly I am so stuck…

    Like

  6. @Pieter: thanks for sharing your findings
    @Christian: I am not sure what is going on here. I personally never used ContainsDataItem method on ViewData because I always used a typed ViewData.

    I have a BaseViewData that is used on the master page and every page using that master and then I create derived classes for ViewData for each page e.g. UserInfoViewData, etc…

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s