简体   繁体   English

RedirectToAction不能满足我的需求。 我有什么选择?

[英]RedirectToAction does not do what I need. What are my options?

In my controller I have something similar to this: 在我的控制器中,我有类似以下内容:

[HttoPost]
public ActionResult MyMethod(MyViewModel myViewModel)   
{  
   //...Some logic  

   return RedirectToAction("SecondMethod", new { ... pupulating view model ... });  
}   

[HttpGet]
public ActionResult SecondMethod()  
{  
    return RedirectToAction("Index", "SomeOtherController");  
}

[HttpPost]
public ActionResult SecondMethod(MyViewModel myViewModel)  
{  
   //...Do Something
   return View("MyView", myViewModel);  
}

Obviously the RedirectToAction from the MyMethod action will never call the HttpPost one which is what I need. 显然,来自MyMethod操作的RedirectToAction将永远不会调用我需要的HttpPost。 The problem is that if I do the following: 问题是,如果我执行以下操作:

return SecondMethod(myViewModel);

instead, the URL that is displayed on the browser is .../.../.../MyMethod and I need it to be .../.../.../SecondMethod 而是,浏览器上显示的URL是... / ... / ... / MyMethod ,我需要将其设置为... / ... / ... / SecondMethod

What can I do so that I can hit the HttpPost action and have the correct URL displayed on the browser? 我该怎么做才能打HttpPost动作并在浏览器上显示正确的URL?

Thanks for your help. 谢谢你的帮助。

You can create a non action method and call it from MyMethod() and SecondMethod() : 您可以创建一个非操作方法,然后从MyMethod()SecondMethod()调用:

private void DoStuff()
{
    //...Do Something
}

public ActionResult MyMethod(MyViewModel myViewModel)   
{  
   //...Some logic
   DoStuff();  

   return RedirectToAction("SecondMethod", new { ... pupulating view model ... });  
}   

[HttpGet]
public ActionResult SecondMethod()  
{  
    return RedirectToAction("Index", "SomeOtherController");  
}

[HttpPost]
public ActionResult SecondMethod(MyViewModel myViewModel)  
{  
   DoStuff();
   return View("MyView", myViewModel);  
}

It sounds like you don't actually need HttpPost; 听起来您实际上并不需要HttpPost; you just need the functionality from that method to be available in a corresponding HttpGet action that accepts an instance of MyViewModel , and returns a view populated by that model. 您只需要该方法中的功能在相应的HttpGet操作中可用即可,该操作接受MyViewModel的实例,并返回该模型填充的视图。 Does that sound right? 听起来对吗?

If so, try something like this: 如果是这样,请尝试以下操作:

[HttpGet]
[ActionName("SecondMethod")]
public ActionResult SecondMethodWithViewModel(MyViewModel myViewModel)
{
    return View("MyView", myViewModel);
}

You can overload your actions by using the ActionNameAttribute , since you can't declare another method with the same signature as your HttpPost action. 您可以使用ActionNameAttribute重载您的操作,因为您不能声明与HttpPost操作具有相同签名的另一个方法。 The RedirectToAction() call and URL will respect the value you set in this attribute, so you can name the method anything you want. RedirectToAction()调用和URL将尊重您在此属性中设置的值,因此您可以根据需要命名该方法。

If you want to post to an action you need to POST to that action. 如果要发布到操作,则需要发布到该操作。 My recommendation would be to change the method in which the action is occurring. 我的建议是更改操作发生的方法。 If it's initiated by a button click, it would be simpler to have multiple forms on your page that post independently or before submitting to use JavaScript to change the action attribute of the form doing the posting. 如果是通过单击按钮启动的,则在页面上具有多个独立发布的表单或在提交之前使用JavaScript更改发布表单的action属性之前,这将更为简单。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM