RenderAction strange side effect

I’m calling Html.RenderAction in my _Layout.cshtml page because I need a partial view to render with its own model. It’s a widget to change the role of any user in my database. I don’t know if this is at all relevant, but it’s rendering a Kendo Window that
is not visible until an image is clicked. There is no form in this partial view – I’m handling the post via a jQuery ajax call:

$.ajax({
   url: url,
   type: 'POST',
   data: { userId: userId, roleId: roleId },
   cache: false,
   success: function (data) {
      $('#RoleChangeFeedback').html('User Role Updated');

      $('#RoleChangeFeedback').show();

      setTimeout(function () {
         $('#RoleChangeFeedback').hide(300);
      }, 1500);
   }
});

Controller methods:

[HttpGet]
[ChildActionOnly]
public PartialViewResult RoleChanger()
{
   // Build model.
   return PartialView(model);
}

[HttpPost]
public void RoleChanger(int userId, int roleId)
{
   new HomeService().UpdateUserRole(userId, roleId);
}

Pretty standard stuff, and it works great… until I navigate to a page where I have a search form (unrelated to the role changer widget). When I try to perform a search, I get the following error message:

The parameters dictionary contains a null entry for parameter ‘userId’ of non-nullable type ‘System.Int32′ for method ‘Void RoleChanger(Int32, Int32)’

For some reason, my RoleChanger post method throws an error when I’m performing my search. Or rather I should say that my RenderAction throws the error, as a break point in my method never gets hit. This has me confused. I tried changing the name of the
post method to ‘GetRoleChanger’, but that caused the following error:

A public action method ‘RoleChanger’ was not found on controller

Why is it trying to fire my post method here? I’m sure this is some odd side effect of RenderAction, which a lot of articles recommend not even using, but since there is no base model to populate the _Layout.cshtml page, I needed it. Any help correcting
this would be very much appreciated.

How is your jquery ajax call triggered?  I’m assuming you have a javascript function wired to the click event of the image? Also, it may be helpful if you could share an example of one of the views that contains the form that is causing the error when submitted.

Hi William,

Please provide the detail code. (E.g. search form view, RoleChanger view)

You may share the project on the OneDrive.

Best Regards

Starain Chen

Leave a Reply