Add Action and Controller to Beginform’s Htmlhelper with T4

Asked

Viewed 65 times

0

I need to add to my template in T4 the value of action and of controller, but I can’t understand how I can pass their names.

Following example:

CREATE.cs.T4

@using (Html.BeginForm("Create", "RamoAtividades", FormMethod.post, new {@class = "form-horizontal"})) 
            {
                @Html.AntiForgeryToken()
                    <div class="form-body">

Where reference "Create" and "Ramoativities", would need to automatically come the values.

How can I do that?

1 answer

1


When you pass null to action and controller it will inherit the names from which acontroller and action that resulted it

@using (Html.BeginForm(null, null, FormMethod.post, new {@class = "form-horizontal"})) 
            {
                @Html.AntiForgeryToken()
                    <div class="form-body">
  • I tried to put the null but when I automatically generated the controller and the views, the values continued as nullin my views.

  • I mean it worked tested here and was, I just did not understand how it works to be null the algorithm understand to which controller it has to send the data. Could you explain? Thank you

  • 1

    Every request you make in the application is mapped by httpContext, so you have stored the controller name and called action. In Htmlhelper when calling Beginform it requires some as controller and action when passing these two null parameters Htmlhelper understands that the form to be compiled has no controller and action and automatically takes these values from the request.

  • 1

    Show thanks so much for the explanation. Thanks for the help

Browser other questions tagged

You are not signed in. Login or sign up in order to post.