0

I have these both actions in my controller:

   public ActionResult Admin()
    {
        var aux=db.UserMessages.ToList();

        return View(aux);         

    }

    public ActionResult Admin(int id)
    {
        var aux = db.UserMessages.ToList();

        return View(aux);

    }

But when I try to access "localhost/Doubt/Admin" I am getting an message saying that its ambiguous... I dont understand why is that... Because if I dont have id in the Url, it should call the first Action withouth the id parameter

2
  • How is your route defined? Commented Apr 2, 2013 at 5:50
  • please post the error Commented Apr 2, 2013 at 5:51

5 Answers 5

2

It is not possible to have 2 actions with the same name on the same controller that are accessible with the same verb (GET in your case). You will have to either rename one of the 2 actions or decorate it with the HttpPost attribute making it accessible only to POST requests. Apparently that's not what you want, so I guess you will have to rename the second action.

Sign up to request clarification or add additional context in comments.

Comments

2

Unless you specify ActionName attribute both actions will be found when "Admin" action is specified. Arguments are not taken into account when matching method to action name.

You can also use HttpGet/HttpPost attributes to specify one to be for GET and another for POST.

 [ActionName("AdminById")]
 public ActionResult Admin(int id)

And in route specify "AdminById" when path contains id.

Comments

0
As you have define two action method with same name,it get confuse about which method to call.
so of you put request first time and in controller you have two method with same name than it will show error like you are currently getting due to it try to find method with attribute HttpGet,but you have not mention that attribute on action method,now when you post your form at that time it will try to find method with HttpPost attribute and run that method,so you have to specify this two attribute on same method name  
    Try this
    [HttpGet]
    public ActionResult Admin()
        {
            var aux=db.UserMessages.ToList();

            return View(aux);         

        }
    [HttpPost]
        public ActionResult Admin(int id)
        {
            var aux = db.UserMessages.ToList();

            return View(aux);

        }

Comments

0

When a user views a page, that's a GET request and when a user submits a form, that's usually a POST request. HttpGet and HttpPost restrict an action method so that the method handles only corresponding requests.

   [HttpGet]
    public ActionResult Admin()
    {
        var aux=db.UserMessages.ToList();

        return View(aux);         

    }

    [HttpPost]
    public ActionResult Admin(int id)
    {
        var aux = db.UserMessages.ToList();

        return View(aux);

    }

In your case if you want to have a get request to the second method, you better rename your method.

Comments

0

In ASP.NET MVC you cannot have two actions with same name and same verb. You can write the code like this to keep your code readability.

private ActionResult Admin()
{
    var aux=db.UserMessages.ToList();
    return View(aux);         

}

public ActionResult Admin(int id = 0)
{
    if (id == 0)
        return Admin();

    var aux = db.UserMessages.ToList();
    return View(aux);

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.