简体   繁体   English

返回相同控制器的不同视图时,URL保持不变

[英]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.

相关问题 从递归返回时字典保持不变 - dictionary stays the same when returning from recursion 在同一时间返回View时是否可以更改URL? 那没有重定向? - Is it possible to change URL when returning View in the same time ? That is without redirecting? .Net MVC控制器使用相同的cshtml作为视图使用不同的路由名称 - .Net MVC Controller different route name using the same cshtml as view 如何从同一视图执行3种不同的控制器方法 - How to execute 3 different controller methods from the same view mvc 5 RedirectToAction不返回视图,而是停留在同一浏览器页面上 - mvc 5 RedirectToAction does not return View, stays on the same browser page 用于重载控制器方法的相同URL - Same url for overloaded controller methods 如何在同一视图中的同一个控制器中执行2个不同的ActionResults? - How do I perform 2 different ActionResults in the same controller within the same view? 当为 Returns() 提供不同的值时,最小起订量返回相同的结果 - Moq Returning Same Result When Supplying Different Values To Returns() SetActualResponseType使用相同方法在同一控制器中使用不同的路由 - SetActualResponseType with same method in the same controller with different routes 查看为 Controller 提供相同的值 - View providing the same value to Controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM