【问题标题】:How to return an existing view from a new action in ASP.NET MVC 4?如何从 ASP.NET MVC 4 中的新操作返回现有视图?
【发布时间】:2014-06-25 23:55:32
【问题描述】:

Noob 需要帮助!)如何从同一控制器中的新操作返回现有视图? 例如我有以下代码:

[HttpGet]
public ActionResult Index()
{
     return View(); //returns Index.cshtml
}

[HttpPost]
public ActionResult Index(string id, string condition)
{
     SomeModel.ID = id;
     SomeModel.Condition = condition;
     return View(SomeModel); //returns Index.cshtml delegating the model
}

public ActionResult someAction()
{
     return View(); //How to make this action return Index.cshtml??
}

【问题讨论】:

  • return View("Index");
  • 我尝试这样做,但遇到错误
  • @user3493623 什么样的错误?
  • 已从客户端收到潜在危险的 Request.Path 值(翻译自俄语)
  • @user3493623 恐怕你的问题是别的。您可能没有向我们展示更多代码,并且与错误有关。

标签: c# asp.net .net asp.net-mvc-4


【解决方案1】:

您可以指定要返回的视图名称:

public ActionResult someAction()
{
     return View("Index"); 
}

【讨论】:

    【解决方案2】:
    public ActionResult someAction()
    {
         return View("Index"); //How to make this action return Index.cshtml??
    }
    

    【讨论】:

      【解决方案3】:

      只需添加

      返回视图("YourView");

      如果你想向它发送模型,你可以这样做

      var 模型 = 新的 YourViewModel {

      }

      return View("YourView", model);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-09-11
        • 2015-12-07
        • 2011-01-23
        • 1970-01-01
        • 1970-01-01
        • 2013-10-30
        • 1970-01-01
        相关资源
        最近更新 更多