【问题标题】:passing parameters in mvc3在mvc3中传递参数
【发布时间】:2012-07-05 05:36:25
【问题描述】:
public ActionResult Index()
{
    Queries q1 = new Queries();
    return View(q1);
}

public ActionResult Index(string id)
{
    Queries q1 = new Queries(id);
    return View(q1);
}

public ActionResult Select(string id)
{
    return RedirectToAction("Index",id);
}

怎么了……

它说: 当前对控制器类型“CompanyController”的操作“Index”的请求在以下操作方法之间不明确: 类型 LookUpForm.Controllers.CompanyController 上的 System.Web.Mvc.ActionResult Index() 类型 LookUpForm.Controllers.CompanyController 上的 System.Web.Mvc.ActionResult Index(System.String)

由于动作索引一个没有参数,另一个有字符串参数,我认为它一定是有效的。

【问题讨论】:

    标签: asp.net-mvc-3 razor


    【解决方案1】:

    您不能在同一个控制器上使用相同名称的 2 个操作,即使它们采用不同的参数。消除它们歧义的唯一方法是使用不同的 HTTP 动词:

    public ActionResult Index()
    {
        Queries q1 = new Queries();
        return View(q1);
    }
    
    [HttpPost]
    public ActionResult Index(string id)
    {
        Queries q1 = new Queries(id);
        return View(q1);
    }
    

    或者如果这两个操作都需要使用 GET 动词访问,您将不得不找到不同的名称或写一个 custom action selector

    【讨论】:

      【解决方案2】:

      只有当它们的 httpmethods 不同时,你才能有动作重载,即 HttpGet 或 HttpPost

      【讨论】:

      • 两者都是HttpGet如何重载动作
      • 两个 HttpGet 操作方法无法使用相同的名称,这就是为什么你得到异常的原因?
      • 是的,我有三个相同的操作方法.. 两个用于获取,一个用于发布。
      猜你喜欢
      • 2012-05-20
      • 2011-09-11
      • 1970-01-01
      • 1970-01-01
      • 2012-02-27
      • 1970-01-01
      • 2012-04-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多