4

The base functionality I wish to achive is that the contents of a table are updated when a dropdownlist item is selected. This will update when the user makes a new selection and retrieve new information from the database and repopulate the table.

It's also worth noting that the DropDownListFor that I want the .change() to work with is not contained within the AjaxForm but appears elsewhere on the page (admittedly in another form)

To achieve this I looked at this question: Rendering partial view dynamically in ASP.Net MVC3 Razor using Ajax call to Action which does a good job of going part the way of what I want to do.

So far, I have a controller method which handles populating a customized viewmodel for the partial view:

    [HttpPost]
    public ActionResult CompanyBillingBandDetails(int id = 0)
    {
        var viewModel = new BillingGroupDetailsViewModel();
        var billingGroupBillingBands =
            _model.GetAllRecordsWhere<BillingGroupBillingBand>(x => x.BillingGroupId == id).ToList();

        foreach (var band in billingGroupBillingBands)
        {
            viewModel.BillingBands.Add(band.BillingBand);
        }

        return PartialView("BillingGroupDetailsPartial", viewModel);
    }

The ViewModel I wish to populate each call:

 public class BillingGroupDetailsViewModel
    {
        public List<BillingBand> BillingBands { get; set; }
    }

The strongly typed model I'm using as a model for the partial view

public class BillingBandsObject
    {
        public int BillingBandId { get; set; }
        public int RangeFrom { get; set; }
        public int RangeTo { get; set; }
        public Decimal Charge { get; set; }
        public int BillingTypeId { get; set; }
        public bool Delete { get; set; }
    }

The partial view it populates and returns:

@model xxx.xxx.DTO.Objects.BillingBandsObject


<tr>
    <td>
        @Html.DisplayTextFor(x => x.RangeFrom)
    </td>
</tr>
<tr>
    <td>
        @Html.DisplayTextFor(x => x.RangeTo)
    </td>
</tr>
<tr>
    <td>
        @Html.DisplayTextFor(x => x.Charge)
    </td>
</tr>

The Razor code for this section of the page:

    <table>
        <thead>
           <tr>
               <th>
                  Range From
               </th>
               <th>
                  Range To
               </th>
               <th>
                  Charge
               </th>
           </tr>
        </thead>
    <tbody>

    @using (Ajax.BeginForm("CompanyBillingBandDetails", new AjaxOptions() { UpdateTargetId = "details", id = "ajaxform" }))
    {
    <div id="details">
        @Html.Action("CompanyBillingBandDetails", new { id = 1 })
    </div>
    }

    </tbody>
 </table>

and finally the function I lifted almost straight from Darin's answer:

$(function () {
        $('#billinggrouplist').change(function () {
            // This event will be triggered when the dropdown list selection changes

            // We start by fetching the form element. Note that if you have
            // multiple forms on the page it would be better to provide it
            // an unique id in the Ajax.BeginForm helper and then use id selector:
            var form = $('#ajaxform');

            // finally we send the AJAX request:
            $.ajax({
                url: form.attr('action'),
                type: form.attr('method'),
                data: form.serialize(),
                success: function (result) {
                    // The AJAX request succeeded and the result variable
                    // will contain the partial HTML returned by the action
                    // we inject it into the div:
                    $('#details').html(result);
                }
            });
        });
    });

At the moment I have fought through a number of errors, currently I am faced with :

"Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'."

However, i feel my understanding of the problem as a whole may be lacking.

Any help appreciated!

Community
  • 1
  • 1
M05Pr1mty
  • 731
  • 9
  • 29
  • You should add your controller definition that surrounds the action method in question. – Chris Marisic Nov 30 '11 at 13:45
  • @ChrisMarisic I dont see why? The only class variable I'm using is the "_model" which is a service layer for the repository i'm using, and theres no issues there. Am i misunderstanding you? – M05Pr1mty Nov 30 '11 at 14:16
  • @NickLarsen I'm afraid I dont know. – M05Pr1mty Nov 30 '11 at 14:16
  • @MattTolliday in the stack trace, you will see it say something like "inner stack trace" or if you are debugging it with visual studio, and catch the exception in the debugger, you can inspect the exception, which has an `InnerException` property. – Nick Larsen Nov 30 '11 at 14:24
  • @NickLarsen That was some very helpful advice; its isolated that the public action method "CompanyBillingBandDetails" was not found on the controller. (Its looking at the correct controller). How odd.... – M05Pr1mty Nov 30 '11 at 14:27
  • I've updated with the solution to that and continuing problem – M05Pr1mty Nov 30 '11 at 14:35
  • @MattTolliday I'm talking about the controller class definition (and chain of classes if needed to reach the built in class). `[Filters][Filters][Filters] MyController : Controller` etc – Chris Marisic Nov 30 '11 at 14:49
  • @ChrisMarisic Okay, updated question with the controller definitions. We're using 3 custom attributes to decorate the controllers handling exceptions, authorisation and system down – M05Pr1mty Nov 30 '11 at 15:07
  • @MattTolliday seeing that you are indeed using AOP, did you try this with commenting out your action filters? Same with short circuiting any base calls for the standard OnResultExecuted and similar methods if you override them? After reading your latest update, I assume your authorization action is triggering a redirect perhaps. – Chris Marisic Nov 30 '11 at 15:09

1 Answers1

0

This error means that there was an exception while rendering your child view. Probably something related to your data, ie. NulLReferenceException.

Just attach your debugger and set to to break when an exception is thrown.

Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
  • I can only debug this issue to a certain point. The @Html.ActionLink inside the Ajax form begins to load by calling the GetControllerInstance in my DI framework but then crashes out to the exception i mentioned. – M05Pr1mty Nov 30 '11 at 14:17
  • @MattTolliday - just attach the debugger and set a breakpoint at the beginning of your action. – Jakub Konecki Nov 30 '11 at 14:43