Passing Form Data to Controller Using Another View in Web API

Introduction

This article describes how to send Form Data to the Controller class. In this article we create a HTML form by a view and pass the value to the controller. The same value will be displayed by another view.

Procedure for creating the application.

Step 1

Create a Web API application as in the following:

  • Start Visual Studio 2012.
  • From the start window select "Installed" -> "Visual C#" -> "Web".
  • Select "ASP.NET MVC4 Web Application" and click on the "OK" button.

form.jpg

  • From the "MVC4 Project" window select "Web API".

form1.jpg

  • Click on the "OK" button.

Step 2

Create a class in the model folder:

  • In the "Solution Explorer".
  • Right-click on the Model folder.
  • Select "Add" -> "Class".
  • Select "Installed" -> "Visual C#" and select class.

form2.jpg

  • Click on the "Add" button.
     

Add the following Code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

 

namespace FormData.Models

{

    public class UserModel

    {

        public string FirstName { get; set; }

        public string LastName { get; set; }

    }

}

Step 3

Create a controller.

  • In the "Solution Explorer".
  • Right-click on the Controller folder.
  • Select "Add" -> "Controller" and change the name.

form3.jpg

  • Click on the "OK" button.

Add the following Code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using FormData.Models;

namespace FormData.Controllers

{

    public class UserController : Controller

    {

        //

        // GET: /User/

 

        public ActionResult DisplayForm()

        {

            return View("Display");

        }

        [HttpPost]

        public ActionResult ShowUser()

        {

            String FirstName = Convert.ToString(Request["FirstName"]);

            String LastName = Convert.ToString(Request["LastName"]);

            UserModel user = new UserModel();

            user.FirstName = FirstName;

            user.LastName = LastName;

            ViewData["UserModel"] = user;

            return View("DisplayUser");

        }

    }

}


In this Controller there are two action methods, one is "DisplayForm" and the other is "ShowUser". The "DisplayForm" is the default method that executes and returns the

"Display" view that is shown by the HTML form.  And the "ShowUser" method calls another view "ShowUser" that returns the value of the "Display" view.

 

Step 4

Now add two MVC4 View Pages (aspx) in the Shared folder, one is "Display.aspx" and the second is "DisplayUser.aspx" as in the following:

  • In the "Solution Explorer".

  • Right-click on the "Shared" folder and select "Add" -> "New Item".

  • Select "Installed" -> "Visual C#" -> "Web" and select MVC4 View Page (aspx).

form4.jpg

  • Click on the "Add" button.

Add the code in the Display.aspx page.

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<FormData.Models.UserModel>" %>

<!DOCTYPE html>

<html>

<head runat="server">

    <meta name="viewport" content="width=device-width" />

    <title>Display</title>

</head>

<body>

    <div>

    <form method="post" action="User/ShowUser">

        Enter FirstName:- <input type="text" id="FirstName" name="FirstName" /> <br />

        Enter LastName:- <input type="text" id="LastName" name="LastName" />

        <input type="submit" value="Submit" />

    </form>

    </div>

</body>

</html>

 

Add the following code in the "DisplayUser.aspx":

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<!DOCTYPE html>

<html>

<head runat="server">

    <meta name="viewport" content="width=device-width" />

    <title>DisplayUser</title>

</head>

<body>

    <div>

     <% var user = (FormData.Models.UserModel)ViewData["UserModel"];%>

       User First Name is :- <%= user.FirstName %> <br />

       User Last Name is :- <%= user.LastName %>

    </div>

</body>

</html>

 

Step 5

Execute the application; the output will be:

form5.jpg

Fill in both TextBoxes and click on the "Submit" button.

form6.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all