开发者

two controllers with same params

开发者 https://www.devze.com 2022-12-23 22:45 出处:网络
i have 2 actions public ActionResult FilesAdd(int id) { FillParentMenuDDL(id); return View(); } [HttpPost] public ActionResult FilesAdd(int id)

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 p开发者_JAVA技巧arameter. 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


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


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


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
<% } %> 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号