gpt4 book ai didi

asp.net-mvc - ASP.NET MVC - 1 个 ActionResult 上的 ActionName 属性重复

转载 作者:行者123 更新时间:2023-12-01 09:35:47 24 4
gpt4 key购买 nike

Possible Duplicate:
ASP.NET MVC - Can i have multiple names for the same action?

我想知道是否有可能让两个 Action 指向 Controller 中的同一个 ActionResult,如下所示:

[HttpPost]
[ActionName("ManualSearch")]
[ActionName("AddProduct")]
public virtual ActionResult MyPostAction(MyModel model)
{

// do code...

}

这样做的原因是因为我有 2 个 View ,每个 View 以不同的方式处理一个 Action ,但它们都包含一个表单,它本质上是在做同样的事情,所以我想只让这 2 个表单发布到同一个 Action ,就像我在上面尝试的那样,只是显然它不可能复制 ActionName 属性。

最佳答案

为什么不创建两个不同的 Action 和一个由两者调用的私有(private)方法?

public ActionResult Action1(MyModel model)
{
return PrivateAction(model);
}

public ActionResult Action2(MyModel model)
{
return PrivateAction(model);
}

private ActionResult PrivateAction(MyModel model)
{
.... do stuff....
}

我认为最简单的事情是最可靠的;)

关于asp.net-mvc - ASP.NET MVC - 1 个 ActionResult 上的 ActionName 属性重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8353424/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com