Using Html.BeginForm to post to the current controller

后端 未结 3 1640
温柔的废话
温柔的废话 2021-01-12 10:37

I have a partial that is used in several views. The partial contains forms. The action when a form is submited is allways the same, but the controller thats contains the act

3条回答
  •  广开言路
    2021-01-12 11:29

    This code will always give your current controller

    <%=( Url.RequestContext.RouteData.GetRequiredString("Controller")) %>
    

    Obviously you can use it without the <%= like this

    Html.BeginForm(
      Url.RequestContext.RouteData.GetRequiredString("Controller")) , 
      "ActOnChoosenPerson") 
    

    It looks more clunky but your model should not need to know what controller is calling it.

提交回复
热议问题