简体   繁体   English

使用POST重新路由到同一控制器中的不同方法

[英]Rerouting to different method in same controller with POST

I have 2 index methods in a controller, one get to load the page initially and one post to accept form submit and reload the same view based on the submitted data. 我在控制器中有2种索引方法,一种可以首先加载页面,另一种可以接受表单提交并根据提交的数据重新加载相同的视图。

    [HttpGet]
    public ActionResult Index()
    {
        loads page with viewmodel containing a searchobject that is submitted with a form on the page
    }

    [ValidateAntiForgeryToken]
    [HttpPost]
    public ActionResult Index(SearchModel Model)
    {
        takes form submit and reloads page based on search results
        return View(Model);
    }

I also have an Add view/method in the same controller. 我在同一控制器中也有一个添加视图/方法。 What I'm wanting to do is when the add function is successful, I'd like to call the POST version of the index and pass it the id of the item that was just added. 我想要做的是add函数成功时,我想调用索引的POST版本,并将刚刚添加的项目的ID传递给它。

here's what I have so far: 这是我到目前为止的内容:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Add(AddModel addmodel)
    {
        do stuff

        var status = client.Add(dto);

        if (!status)
        {
            return View(AddModel);
        }
        SearchModel = new SearchModel();
        SearchModel.ID= addmodel.ID;
        return Index(search, null);
    }               

It enters the POST index method but after it leaves that method it goes back to the add and tries to load the add page with the search viewmodel and obviously throws an exception. 它进入POST索引方法,但是在离开该方法之后,它返回到add并尝试使用搜索viewmodel加载add页面,并且显然引发异常。

Any ideas what my options are to do something like this? 有什么想法我可以做这样的事情吗?

By default View(Model) uses name of current action (the one routed to - Add in your case, not the current method name - Index ). 默认情况下, View(Model)采用电流动作(一排到-的名字Add在你的情况,而不是当前方法的名称- Index )。

Fix: specify view name explicitly: 修复:明确指定视图名称:

public ActionResult Index(SearchModel Model)
{
    takes form submit and reloads page based on search results
    return View("Index", Model);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在同一控制器上调用不同方法的控制器方法 - A controller method that calls a different method on the same controller SetActualResponseType使用相同方法在同一控制器中使用不同的路由 - SetActualResponseType with same method in the same controller with different routes 控制器中的相同方法接受 GET 和 POST - Same method in controller accept GET and POST 从同一控制器中的另一个方法调用post方法 - Calling post method from another method in the same controller 如何为具有不同参数的同一Controller方法设置不同的路由? - How to set different routes for the same Controller method with different parameters? ASP MVC Controller发布具有不同值的相同html输入名称 - ASP MVC Controller post same html input name with different values 两个HTTP Post方法在一个Web API控制器中具有不同的参数 - Two HTTP Post method with different parameters in one Web API controller 在同一个Controller中使用Get和Post - Use Get And Post in the same Controller 找不到OData控制器上的发布方法 - Post method on OData controller not found @ Url.Action(“ Action”,“ Controller”)调用具有相同名称的post和get方法 - @Url.Action(“Action”,“Controller”) calls both post and get method with same name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM