HttpPost之后,当Model无效时返回一个View(ActionResult),响应的内容类型为application/json
我在 ASP.NET MVC 站点的多个页面中遇到了一个奇怪的问题。当我发布表单并且模型无效时,我尝试返回相同的视图,以便我可以看到错误 - 但是,我没有重新加载页面,而是收到一个弹出下载框,显示该文件是以“application/json”格式。正如您从下面的代码中看到的,控制器方法返回一个 ActionResult 而不是 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);
}
当我提交表单而不填写表单时,我可以看到模型验证失败(正确),但是当它到达最后一行时“返回视图(模型);” - 它返回我期望的所有 HTML - 但内容类型设置为“application/json”。我没有在代码中的任何位置设置内容类型 - 所以我无法弄清楚为什么会发生这种情况。其他页面上也发生了同样的事情,所以我认为我做错了一些基本的事情 - 但我似乎无法弄清楚。
有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我终于弄清楚了这个问题......这是我引入的错误,我很不好意思地说。然而,这是一个非常容易犯的错误,所以我想在这里记录这个问题,以防其他人遇到它。这都是由于我在“Site.Master”页面上使用的“Html.RenderAction(..)”调用引起的。该操作返回一个 JsonResult - 如果我尝试执行的原始帖子遇到错误 - 那么返回 JsonResult 的操作也会在加载母版页后立即执行 - 从而导致此问题。
我最终删除了“Html.RenderAction(...)”调用 - 并只是硬编码了我需要的 HTML。
希望有帮助
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