MVC Jquery Ajax call returning a view that claims it was passed the wrong model
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 = UrlParamete开发者_C百科r.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
);
}
I guess you are passing a wrong model to the view in your CouponCampaignTable
action:
public ActionResult CouponCampaignTable()
{
CouponCampaign model = ...
return PartialView(model);
}
and of course the CouponCampaignTable.cshtml
partial should be strongly typed to CouponCampaign
:
@model CouponCampaign
...
Or maybe the problem is with your initial $.post
request to the Add
controller action:
public ActionResult Add()
{
SystemMessage model = ...
return PartialView(model);
}
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:
return PartialView("SuccessMessage", viewModel);
and that did the trick.
精彩评论