views:

32

answers:

3

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

+1  A: 

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();
}
Developer Art
Since he uploads one file, `HttpPostedFileBase name` parameter would be more appropriate.
LukLed
This feels a bit hackish to me to add a redundant parameter that is never used.
David G
A: 

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
<% } %> 
Shay Erlichmen
+3  A: 

.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();
}
David G