I'm running into a strange issue in more than one page of my ASP.NET MVC site. When I POST a form and the Model is NOT valid, I try to return the same view so that I can see the errors - however, instead of the page getting reloaded, I get a pop-up download box that says that the file is in "application/json" format. As you can see from the code below, the controller method returns an ActionResult and NOT a JsonResult:
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
var isValid = IsUserAuthenticated(model);
if (isValid)
{
if (!String.IsNullOrEmpty(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return User.IsInRole("Administrator")
? RedirectToAction("Index", "Admin")开发者_开发问答
: RedirectToAction("Index", "Home");
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
When I submit my form without filling it out, I can see that the Model fails validation (correctly), but when it reaches the last line "return View(model);" - it returns all the HTML that I expect - but the content type is set to "application/json". I don't set the content-type anywhere in my code - so I can't figure out why this happening. The same thing is happening on other pages as well, so I'm thinking that there's some fundamental thing that I'm doing wrong - but I can't seem to figure it out.
Any thoughts?
I finally figured out the issue... it was an error introduced by me, I'm embarrassed to say. However, it's a really easy mistake to make, so I wanted to document the issue here in case anyone else encounters it. It was all caused because of an "Html.RenderAction(..)" call that I was using on the "Site.Master" page. That action returns a JsonResult - and if the original post I was trying to do encountered errors - then the action that returns the JsonResult would also execute as soon as the master page was loaded - thus causing this issue.
I ended up removing the "Html.RenderAction(...)" call - and just hard coded the HTML I needed there.
Hope that helps
精彩评论