[英]URL stays same when returning different view of same controller
I am trying to return a different view from my controller. 我正在尝试从控制器返回不同的视图。 However, although the correct view is displayed the URL stays same.
但是,尽管显示了正确的视图,但URL保持不变。
This is my form in /Company/Create
view. 这是我在
/Company/Create
视图中的表单。
@using (Html.BeginForm("Create", "Company", FormMethod.Post))
{
// Form here
}
So basically, the form and model is submitted to /Company/Create
action. 因此,基本上,表单和模型将提交给
/Company/Create
操作。 If the submitted model is valid, then I process the data and redirect to /Company/Index view with 如果提交的模型有效,那么我将处理数据并使用重定向到/ Company / Index视图
return View("Index");
As I said, correct view is displayed however, URL (address bar) is still http://.../Company/Create
如我所说,但是显示的是正确的视图,URL(地址栏)仍然是
http://.../Company/Create
I tried RedirectToAction("Index");
我尝试了
RedirectToAction("Index");
It does not work also. 它也不起作用。 And I do not think its a good MVC practice.
而且我认为这不是MVC的良好做法。 I have a single layout and Company views are rendered with
RenderBody()
我只有一个布局,并且使用
RenderBody()
渲染了公司视图
Any ideas ? 有任何想法吗 ?
Thanks. 谢谢。
Edit : 编辑:
This is my action method, 这是我的行动方法
[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);
}
You need to redirect to another action if you want the url changed. 如果要更改URL,则需要重定向到其他操作。
However RedirectToAction doesn't redirect instantly but returns a RedirectToRouteResult
object which is an ActionResult
object. 但是, RedirectToAction不会立即重定向,而是返回一个
RedirectToRouteResult
对象,它是一个ActionResult
对象。
So you just need to return the result of RedirectToAction
from your action: 因此,您只需要从操作中返回
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.