【问题标题】:Redirect to same view重定向到同一视图
【发布时间】:2012-12-15 18:45:49
【问题描述】:

我在一个 ASP.NET MVC 网站上工作,我是新手。 我有一个几乎没有动作的控制器。我想在我的网站上使用这些操作。 例如

[HttpPost]
public ActionResult MyAction(ViewModel model)
{

    if (ModelState.IsValid)
    {
        //code is here
    }
  return RedirectToAction(); // redirect to same view

}

我想重定向到生成请求的同一视图。我不确定这是否可能?

【问题讨论】:

  • 只要返回 View() 就可以了...
  • 您想要的都是可能的,但是由于您的问题含糊不清,很难理解您希望它在什么环境下工作。我假设您想保留您的代码DRY,但您是要重用 Action Logic 还是 View Result?
  • @Erik Philips。我想重用我的视图结果和操作逻辑
  • @Bhushan 我会试一试的。

标签: asp.net-mvc controller redirecttoaction


【解决方案1】:

根据您的评论,我将创建一个如下所示的控制器:

public MyController : Controller
{
  private ActionResult SharedMethod(SomeModel model)
  {
    if (ModelState.IsValid)
    {
      //code is here
    }

    // viewname is required, otherwise the view name used will be
    // the original calling method (ie public1.cshtml, public2.cshtml)
    return this.View("SharedViewName");
  }

  public ActionResult Public1(SomeModel model)
  {
    return this.SharedMethod(model);
  }

  public ActionResult Public1(SomeModel model)
  {
    return this.SharedMethod(model);
  }
}

【讨论】:

  • 实际上我有一个视图,其中列出了数据库中产品的所有详细信息。单击一个按钮会打开一个弹出窗口,其中包含一个表单。所以我想使用这种常见的操作方法来发布 cmets 并将用户重定向到更新后的 cmets 的同一视图(产品页面)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-07
  • 2016-04-02
  • 2020-01-01
  • 2012-03-10
  • 2017-10-30
相关资源
最近更新 更多