【问题标题】:How to Overload ActionResult in Asp.Net MVC2如何在 Asp.Net MVC2 中重载 ActionResult
【发布时间】:2011-03-08 12:28:42
【问题描述】:

MVC2 中的 ActionResult 方法重载有问题

我有 3 种方法

public ActionResult MyMethod()
{  
   var data = ........
   //some unfiltered data from db
   return view(data);
}

public ActionResult MyMethod(string name)
{
   var data = .......
              Where xxx.StartsWith(name)
   //some filtered data by name
   return View(data);
}

public ActionResult MyMethod(int age)
{
   var data = .......
              Where xxx.Equals(age)
   //some filtered data by age
   return View(data);
}

如何在 Asp.Net MVC2 中重载方法? 谢谢。

【问题讨论】:

    标签: asp.net-mvc-2 c#-4.0


    【解决方案1】:

    简短的回答,您不能仅通过变量重载方法。

    Discussion on Stack

    如果您必须具有相同的方法名称,则需要创建一个 actionfilter 属性并将其用作您的重载。

    以上讨论的片段:

    [RequireRequestValue("someInt")]
    public ActionResult MyMethod(int someInt) { /* ... */ }
    
    [RequireRequestValue("someString")]
    public ActionResult MyMethod(string someString) { /* ... */ }
    

    【讨论】:

    • 其中命名空间是[RequireRequestValue()]
    • 您必须自己创建操作过滤器并随意调用它。重新阅读我发布的问题,看看@他是如何解决他的困境的。
    • 如果我想使用 Mymethod(string a, string b, string c),你能告诉我过滤器示例吗?谢谢
    • 到目前为止,您尝试过哪些方法来重载具有多个参数的方法?我会帮你调试你的代码,但不是给你写的……
    猜你喜欢
    • 2013-11-19
    • 1970-01-01
    • 2011-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多