Showing posts with label Use of Action in Form Tag in MVC. Show all posts
Showing posts with label Use of Action in Form Tag in MVC. Show all posts

Monday, 4 November 2013

Use of Action in Form Tag in MVC

Use of Action in Form Tag in MVC

In my last article I explained the Form Tag and use of GET and POST method. In this article I am going to explain the use of Action Method in MVC.

Action is the element of Form Tag and it is used to redirect page when we submit the button.

Syntax of Action:-

 Action=”Controller\View”

For example:-

I have created two controllers in my MVC application. One is Home controller and another is Welcome controller.

From Home Controller after pressing the submit button I want it to redirect it on Welcome Controller. For this in Form Tag I have to add the action element and setting the controller and view.

Code of Index View of Home Controller

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <form method="post" action="Welcome\Index">
        <table>
            <tr>
                <td>Enter First Name</td>
                <td><input type="text" id="fname_id" name="fname_name" /></td>
            </tr>
             <tr>
                <td>Enter Last Name</td>
                <td><input type="text" id="lname_id" name="lname_name" /></td>
            </tr>
             <tr>
                <td></td>
                <td><input type="submit"  /></td>
            </tr>
        </table>
    </form>
</body>
</html>