Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# asp.net mvc中的操作不明确,如何解决?_C#_Asp.net_.net_Asp.net Mvc - Fatal编程技术网

C# asp.net mvc中的操作不明确,如何解决?

C# asp.net mvc中的操作不明确,如何解决?,c#,asp.net,.net,asp.net-mvc,C#,Asp.net,.net,Asp.net Mvc,我有两种方法: public ActionResult Index(int? id) { return Redirect("/View/" + this.GetMenuItems().First().Id); } public ActionResult Index(int id, uint? limit) { 当我转到/View/1-我得到了那个错误 当前对控制器类型“ViewController”的操作“Index”的请求在以下操作方法之间不明确: SVNViewer.Contro

我有两种方法:

public ActionResult Index(int? id)
{
    return Redirect("/View/" + this.GetMenuItems().First().Id);
}

public ActionResult Index(int id, uint? limit)
{
当我转到/View/1-我得到了那个错误

当前对控制器类型“ViewController”的操作“Index”的请求在以下操作方法之间不明确: SVNViewer.Controllers.ViewController类型上的System.Web.Mvc.ActionResult索引(System.Nullable
1[System.Int32])
SVNViewer.Controllers.ViewController类型上的System.Web.Mvc.ActionResult索引(Int32,System.Nullable
1[System.UInt32])


我需要这两种方法,但要删除不明确的错误,我如何才能做到这一点?

将第二个操作更改为不具有可为null的uint

public ActionResult Index(int id, uint limit)

您的
索引(int?id)
应该是处理limit是否为null的方法。

将第二个操作更改为不具有可为null的uint

public ActionResult Index(int id, uint limit)

您的
索引(int?id)
应该是处理limit是否为null的方法。

您可以使用ActionName解决此问题:

如果两者都做相同的事情,您可以改为:

public ActionResult Index(int? id, uint? limit = null)
{
  ...
}
将第二个参数设为可选


或者一个具有[HttpGet]属性,另一个具有[HttpPost],如果一个响应get,另一个响应发布的表单。

您可以使用ActionName解决此问题:

如果两者都做相同的事情,您可以改为:

public ActionResult Index(int? id, uint? limit = null)
{
  ...
}
将第二个参数设为可选


或者一个有[HttpGet]属性,一个有[HttpPost],如果一个响应get,另一个响应发布的表单。

您可以创建一个模型,并且只有一个操作方法:

public class MyActionModel 
{
    public int? Id { get;set; }
    public int? Limit { get;set; }
}
然后在控制器中:

public ActionResult Index(MyActionModel model)
{
    // Your code here
    if (model.Id.HasValue() { // do something....etc}
}

您可以创建一个模型,并且只有一个操作方法:

public class MyActionModel 
{
    public int? Id { get;set; }
    public int? Limit { get;set; }
}
然后在控制器中:

public ActionResult Index(MyActionModel model)
{
    // Your code here
    if (model.Id.HasValue() { // do something....etc}
}

将其中一个方法重命名为其他方法:将第二个方法重命名为Index->MenuItem?或者,在第二行中设置limit参数required(不可为null)。@Tommy如果您作为一个选项回答,我会投您一票answer@Mark-我很感激,但是当我遇到这个问题时,我没有足够的时间来制定一个好的答案,但是我想为testCoder投入2美分!将其中一个方法重命名为其他方法:将第二个方法重命名为Index->MenuItem?或者,在第二行中设置limit参数required(不可为null)。@Tommy如果您作为一个选项回答,我会投您一票answer@Mark-我很感激,但是当我遇到这个问题时,我没有足够的时间来制定一个好的答案,但是我想为testCoder投入2美分!