C# MVC 5-重定向到操作未重定向
嗨,我的重定向操作有问题,没有重定向。我的代码成功地命中了重定向上的断点,因此我可以确认它正在被调用。然而,似乎什么也没有发生 我试着查看Chrome中的网络流量,但似乎没有任何明显的变化。我一定错过了一些简单的东西C# MVC 5-重定向到操作未重定向,c#,asp.net-mvc-5,C#,Asp.net Mvc 5,嗨,我的重定向操作有问题,没有重定向。我的代码成功地命中了重定向上的断点,因此我可以确认它正在被调用。然而,似乎什么也没有发生 我试着查看Chrome中的网络流量,但似乎没有任何明显的变化。我一定错过了一些简单的东西 // // POST: /Blog/CreateBlog [HttpPost] [ValidateAntiForgeryToken] public ActionResult CreateBlog(BlogViewModel model)
//
// POST: /Blog/CreateBlog
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateBlog(BlogViewModel model)
{
var userId = User.Identity.GetUserId();
model.UserId = userId;
if (ModelState.IsValid && model.UserId != null)
{
Mapper.CreateMap<BlogViewModel, Blog>();
if (_blogProcess.CreateBlog(Mapper.Map<BlogViewModel, Blog>(model)))
{
RedirectToAction("Index", "Blog");
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
//
//POST:/Blog/CreateBlog
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateBlog(BlogViewModel模型)
{
var userId=User.Identity.GetUserId();
model.UserId=UserId;
if(ModelState.IsValid&&model.UserId!=null)
{
CreateMap();
if(_blogProcess.CreateBlog(Mapper.Map(model)))
{
重定向到操作(“索引”、“博客”);
}
}
//如果我们走到这一步,有些东西失败了,重新显示形式
返回视图(模型);
}
试试看
除了Nalaka526的回答之外:如果我们查看for
RedirectToAction
,我们可以看到它是Controller
类的一个实例方法,该类具有as返回类型,它派生自该类,表示我们必须返回它,就像我们返回View
和PartialView
一样
如果您在查看页面上使用@using(Html.BeginForm(“Index”,“Blog.”),那么
public ActionResult CreateBlog(BlogViewModel model)
{
....
return RedirectToAction("Index", "Blog")
}
应该有用
如果您使用的是Ajax.BeginForm
,则需要从javascriptOnSuccess
事件重定向到新的操作/url。
视图的示例代码
@using (Ajax.BeginForm("Index", "Blog", new AjaxOptions { HttpMethod = "post" , OnSuccess="RedirectFunction"}
function RedirectFunction(data)
{
window.location.href = “Index”;
}
希望这能有所帮助。Jeebus…忽略这个小关键词是一记严重的耳光。谢谢你检查我们的理智!
@using (Ajax.BeginForm("Index", "Blog", new AjaxOptions { HttpMethod = "post" , OnSuccess="RedirectFunction"}
function RedirectFunction(data)
{
window.location.href = “Index”;
}