Using T4MVC, how to set the action attribute on an HTML form

1.1k Views Asked by At

I am taking my first teetering steps with submitting Html forms using jQuery. All works well, but would like to use the T4MVC to generate the action link.

This works with Html.BeginForm (and Ajax.BeginForm) because they take an ActionResult as the action generating param. Ie:

Is there a way to do:

<form method="POST" action="@MVC.???">

I suppose I could do:

@using (Html.BeginForm(MVC.MyArea.MyController.MyAction(),...,new {@id="myForm"}))
        {
            // Inputs
        }

But really wonder if T4MVC can handle this. Suspect not, but am new to it, so maybe am missing something?

(And yes, I know about Ajax.BeginForm, but am using the current project to learn more about MVC and jQuery).

2

There are 2 best solutions below

1
On BEST ANSWER

The following should work:

<form method="POST" action="@Url.Action(MVC.MyArea.MyController.MyAction())">

Or if you need to add extra route values:

<form method="POST" action="@Url.Action(MVC.MyArea.MyController.MyAction().AddRouteValues(new { @id = "myForm" }))">
0
On

I use MVCFutures for this. Then, you can do stuff like:

@using (Html.BeginForm<ControllerController>(c => c.MyAction(null))) { // form }

Forms will generally post to c.Action(null) as these actions will often bind to a model, represented here by null.

Caveat: MVC Futures BeginForm does not play nicely with MVCValidation (client-side). If you are using jquery client-side validation you won't have problems.