Showing posts with label Form Tag in MVC. Show all posts
Showing posts with label 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>

Form Tag in MVC View and use of GET and Post method in MVC


Form Tag with some input tag in MVC View and use of GET and POST Method in MVC

In MVC we use GET and POST Method to Each Request and Reply. If we do not set any method for MVC Application then it uses GET Method by Default.

For Example:-

Create a simple MVC Application. Create Home controller and create index view. Simply print Some String in this view and Execute this code.

Note: - To understand basic of MVC kindly read my previous articles: - Controller in MVC and View in MVC

After executing the code you will get following output.



Figure 1

Now press the F12 to open the DevTool. Developers Tool is a Debugging Tool in Chrome which provides accessibility of Web Application in Detail.



Figure 2

Now create another view in this application. Create another method name Welcome in Home Controller and create view for this method. Execute the application and request the Welcome View and then press F12