I’m using Facebox for my dialog boxes. And I have a create form inside one of these Faceboxes. When the create form is submitted via ajax $.post(), the action returns a partial view with a success message to replace the view in the facebox. The issue I’m having is firebug is reporting a 500 server error when the ajax call is completed saying:
The model item passed into the dictionary is of type ‘Models.ViewModels.SystemMessage‘, but this dictionary requires a model item of type ‘Models.CouponCampaign‘.
Here is the partial view for the success message:
@model Redeemupon.Models.ViewModels.SystemMessage
<div class="successMessage" title="Success">
<img src="/Content/Images/Positive_48x48.png" alt=":-)"/>
@Html.Raw(Model.Message)
</div>
And heres the snippet that’s passing this partial view.
var viewModel = new SystemMessage()
{
Message = message
};
return PartialView(viewModel);
And finally, the ajax call
$("#couponCampaignForm").submit(function () {
var queryString = $(this).serialize();
var action = "/CouponCampaign/Add?" + queryString;
$.post(action, function (data) {
//Load the resulting partial view into a facebox
$.facebox(data);
//Refresh the table
var action = "/CouponCampaign/CouponCampaignTable";
$.get(action, function (data) {
$("#ajaxTable").html(data);
});
});
return false;
});
The original view that’s loaded into the facebox uses Models.CouponCampaign is it possible that the second view is trying to inherit that model? It’s supposed to be replaced by the new view, with its own viewmodel.
Heres the routing rules from my globals.asax:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"RegisterStaff", // Route name
"Account/RegisterStaff/{tid}/{email}" // URL with parameters
);
routes.MapRoute(
"ForgotPassword", // Route name
"Account/ForgotPassword/{email}" // URL with parameters
);
}
edit
SO, since I was calling SuccessMessage(string message) from another action, and returning the partial view to that action, I needed to explicitly declare which partial view I want to be returning, since once the PartialViewResult bubbled up to the originating action, it called the wrong partial view.
so I needed:
and that did the trick.