Asked  7 Months ago    Answers:  5   Viewed   39 times

I'm curious to see if you can overload controller methods in ASP.NET MVC. Whenever I try, I get the error below. The two methods accept different arguments. Is this something that cannot be done?

The current request for action 'MyMethod' on controller type 'MyController' is ambiguous between the following action methods:

 Answers

29

You can use the attribute if you want your code to do overloading.

[ActionName("MyOverloadedName")]

But, you'll have to use a different action name for the same http method (as others have said). So it's just semantics at that point. Would you rather have the name in your code or your attribute?

Phil has an article related to this: http://haacked.com/archive/2008/08/29/how-a-method-becomes-an-action.aspx

Tuesday, June 1, 2021
 
mario
answered 7 Months ago
99

Not sure why the Import Namespace directive wasn't doing the trick, but I added

<add namespace="MyProject.HtmlHelpers"/>

to the <namespaces> section of web.config and it's working now. Maybe if I had done

<%@ Import Namespace="MyProject.HtmlHelpers" %>

it would have worked as well?

Friday, August 20, 2021
 
csi
answered 4 Months ago
csi
28

GET requests could be automatically cached by the browser so you could use the .ajax() function which contrary to the .get() function allows you to disabled caching:

$.ajax({
    url: '/foo',
    type: 'GET',
    cache: 'false',
    success: function(result) {

    }
});

Another possibility is to use POST:

$.post('/foo', function(result) {

});
Tuesday, September 14, 2021
 
u.gen
answered 3 Months ago
35

You'll have to reflect on the Controller type itself, using GetCustomAttributes. Use ViewContext.Controller to get a reference to the controller itself. Something like this:

string controllerName;
Type type = ViewContext.Controller.GetType();
var atts = type.GetCustomAttributes(typeof(DisplayNameAttribute), false);
if (atts.Length > 0)
    controllerName = ((DisplayNameAttribute)atts[0]).DisplayName;
else 
    controllerName = type.Name;   // fallback to the type name of the controller

Edit

To do similar for an action, you need to first reflect on the method, using Type.GetMethodInfo:

string actionName = ViewContext.RouteData.Values["Action"]
MethodInfo method = type.GetMethod(actionName);
var atts = method.GetCustomAttributes(typeof(DisplayNameAttribute), false);
// etc, same as above
Thursday, October 21, 2021
 
Aran-Fey
answered 2 Months ago
86

Okay guys, the problem is not about the way you implement this scenario, the problem here is ModelState. I POST to the Action and return the same view. The second time the view is rendered it will look at the ModelState and use those values to fill the controls. So simply we need to clear the ModelState before returning the View.

Model.cs

public class TestModel {
    public String Color { get; set; }
}

Controller.cs

public ActionResult Index() {
        var model = new TestModel();
        model.Color="Blue";
        ViewData["Colors"]=new List<SelectListItem>() { new SelectListItem() { Text =  "Blue", Value = "Blue" }, new SelectListItem() { Text = "Red", Value = "Red" } };


        return View(model);
    }

[HttpPost]
public ActionResult Index(TestModel model) {
        model.Color="Red";
        ViewData["Colors"]=new List<SelectListItem>() { new SelectListItem() { Text =  "Blue", Value = "Blue" }, new SelectListItem() { Text = "Red", Value = "Red" } };

        ***ModelState.Clear();***
        return View(model);
}

Index.cs

@using (Html.BeginForm()) {
@Html.DropDownListFor(m => m.Color, ViewData["Colors"], new { @class = "w200" })
<input type="submit" />

}

Cheeeeeers

Sunday, December 5, 2021
 
van_folmert
answered 4 Days ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share