kusanagi
kusanagi

Reputation: 14614

two controllers with same params

i have 2 actions

public ActionResult FilesAdd(int id)
    {
        FillParentMenuDDL(id);
        return View();
    }

    [HttpPost]
    public ActionResult FilesAdd(int id)
    {
        //some logic...
        FillParentMenuDDL(id);
        return View();
    }

but it is error because of same parameters, but i need only one parameter. first i call page /action/id and then i submit it for example with id and uploaded file, but i access to file using request.files[0]. so what the solution with controllers and same parameters? i see only declare FilesAdd(int? id) in one controller

Upvotes: 1

Views: 179

Answers (3)

David Glenn
David Glenn

Reputation: 24522

.Net MVC has an ActionNameAttribute for this purpose. Rename your second action to something like FilesAddPost and then use ActionNameAttribute("FilesAdd")

public ActionResult FilesAdd(int id)
{
    FillParentMenuDDL(id);
    return View();
}

[HttpPost]
[ActionName("FilesAdd")]
public ActionResult FilesAddPost(int id)
{
    //some logic...
    FillParentMenuDDL(id);
    return View();
}

Upvotes: 3

Shay Erlichmen
Shay Erlichmen

Reputation: 31928

You can control the action of the submitted form, it doesn't have to go to the same action.

// Works under MVC 2.0 
<% using (Html.BeginForm("action", "controller", FormMethod.Post)) { %> 
// code
<% } %> 

Upvotes: 0

user151323
user151323

Reputation:

Add an (unused) form parameter to the POST action. That will make the method signatures different.

[HttpPost]
public ActionResult FilesAdd(int id, FormCollection form)
{
    //some logic...
    FillParentMenuDDL(id);
    return View();
}

Upvotes: 1

Related Questions