[英]URL stays same when returning different view of same controller
我正在尝试从控制器返回不同的视图。 但是,尽管显示了正确的视图,但URL保持不变。
这是我在/Company/Create
视图中的表单。
@using (Html.BeginForm("Create", "Company", FormMethod.Post))
{
// Form here
}
因此,基本上,表单和模型将提交给/Company/Create
操作。 如果提交的模型有效,那么我将处理数据并使用重定向到/ Company / Index视图
return View("Index");
如我所说,但是显示的是正确的视图,URL(地址栏)仍然是http://.../Company/Create
我尝试了RedirectToAction("Index");
它也不起作用。 而且我认为这不是MVC的良好做法。 我只有一个布局,并且使用RenderBody()
渲染了公司视图
有任何想法吗 ?
谢谢。
编辑:
这是我的行动方法
[HttpPost]
public ActionResult Create(CompanyCreate model)
{
/* Fill model with countries again */
model.FillCountries();
if (ModelState.IsValid)
{
/* Save it to database */
unitOfWork.CompanyRepository.InsertCompany(model.Company);
unitOfWork.Save();
RedirectToAction("Index");
return View();
}
// If we got this far, something failed, redisplay form
return View(model);
}
如果要更改URL,则需要重定向到其他操作。
但是, RedirectToAction不会立即重定向,而是返回一个RedirectToRouteResult
对象,它是一个ActionResult
对象。
因此,您只需要从操作中返回RedirectToAction
的结果:
[HttpPost]
public ActionResult Create(CompanyCreate model)
{
/* Fill model with countries again */
model.FillCountries();
if (ModelState.IsValid)
{
/* Save it to database */
unitOfWork.CompanyRepository.InsertCompany(model.Company);
unitOfWork.Save();
return RedirectToAction("Index");
}
// If we got this far, something failed, redisplay form
return View(model);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.