Mapping individual buttons on ASP.NET MVC View to controller actions

后端 未结 3 2123
北荒
北荒 2021-02-08 18:01

I have an application where I need the user to be able to update or delete rows of data from the database. The rows are displayed to the user using a foreach loop in the .aspx

3条回答
  •  渐次进展
    2021-02-08 18:40

    You can also have multiple "valid-named" buttons on the form like:

    
    

    and than check to see what submit you have received. There can be only one submit action sent per form, so it is like all the other submit buttons did not actually existed in the first place:

    if ( HttpContext.Request.Form["btnDelete"] != null ) {
        //Delete stuff
    } elseif ( HttpContext.Request.Form["btnSave"] != null ) {
        //Update stuff
    }
    

    I also think that you can implement a custom ActionMethodSelectorAttribute like here http://weblogs.asp.net/dfindley/archive/2009/05/31/asp-net-mvc-multiple-buttons-in-the-same-form.aspx (also listed above) to have cleaner separated code.

提交回复
热议问题