How to use Forms in MVC?

This post is a continuation from
1. Controller
http://blogs.hypatiasoftwaresolutions.in/how-to-create-a-controller-in-asp-net-mvc/
2. View, ViewData, ViewBag, ViewModels
http://blogs.hypatiasoftwaresolutions.in/how-to-add-a-view-to-a-controller/

Think about the Controller as the classic code behind in WebForms. In this post we shall use the get and post methods directly to the controller.
First of all change the View from the previous page and create a form. Store the model values in the textboxes.
View

@using MVCOne.Models
@model Book
@{
Layout = null;
}
<!DOCTYPE html>
<html><head>
<meta name="viewport" content="width=device-width" />
<title>GetBook</title>
</head>
<body>
<div>
<form>
Book Name <input type="text" value="@Model.BookName" name="bookName"/>
<br />
Author <input type="text" value="@Model.Author" name="author"/>
<br />
Price  <input type="text" name="price" value="@Model.Price"/>
<br />
<input type="submit" value="Send"/>
</form>
</div>
</body>
</html>

Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCOne.Models;
namespace MVCOne.Controllers
{
public class ChampakController : Controller
{
public string GetStringData()
{
return "This is a string from Get String Data";
}
public ActionResult GetBook()
{
string bookname = Request.QueryString["bookName"];
string author = Request.QueryString["author"];
int price = 0;
try
{
price = Convert.ToInt32(Request.QueryString["price"]);
}
catch
{
}
Book b = new Book(bookname, author, price);
return View("GetBook",b);
}
}
}

Debug the project and view the results.

Change the form method to post and the code in the Controller to Request.Form instead of Request.QueryString.
View

<form method="post">
Book Name <input type="text" value="@Model.BookName" name="bookName"/> <br />
Author <input type="text" value="@Model.Author" name="author"/><br />
Price <input type="text" name="price" value="@Model.Price"/><br />
<input type="submit" value="Send"/>
</form>

Controller

public ActionResult GetBook() public ActionResult GetBook()
{
string bookname = Request.Form["bookName"];
string author = Request.Form["author"];
int price = 0;
try
{
price = Convert.ToInt32(Request.Form["price"]);
}
catch
{
}
Book b = new Book(bookname, author, price);
return View("GetBook",b);
}

Finally, we develop a View that has two forms in it. One runs via Get and the other via Post.

View

@using MVCOne.Models
@model Book@{    Layout = null;}
<!DOCTYPE html>
<html><head><meta name="viewport" content="width=device-width" />    <title>GetBook</title>
</head>
<body>
<div>
<h3>Post</h3>
<form method="post" action="/Champak/PostBook">
Book Name <input type="text" value="@Model.BookName" name="bookName" /> <br />
Author <input type="text" value="@Model.Author" name="author" />
<br />
Price  <input type="text" name="price" value="@Model.Price" />
<br />
<label>@Model.RequestType</label>
<br />
<input type="submit" value="Send" />
</form>
<h3>Get</h3>
<form method="get" action="/Champak/GetBook">
Book Name <input type="text" value="@Model.BookName" name="bookName" /> <br />
Author <input type="text" value="@Model.Author" name="author" />
<br />
Price  <input type="text" name="price" value="@Model.Price" />
<br />
<label>@Model.RequestType</label>
<br /><input type="submit" value="Send" />
</form>
</div>
</body>
</html>

Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCOne.Models;namespace MVCOne.Controllers
{
public class ChampakController : Controller
{
public string GetStringData()
{
return "This is a string from Get String Data";
}
[HttpGet]
public ActionResult GetBook()
{
string bookname = Request.QueryString["bookName"];
string author = Request.QueryString["author"];
int price = 0;
try
{
price = Convert.ToInt32(Request.QueryString["price"]);
}
catch
{
}
Book b = new Book(bookname, author, price,"Get");
return View("GetBook",b);
}
[HttpPost]
public ActionResult PostBook()
{
string bookname = Request.Form["bookName"];
string author = Request.Form["author"];
int price = 0;
try
{
price = Convert.ToInt32(Request.Form["price"]);
}
catch
{
}
Book b = new Book(bookname, author, price, "Post");
return View( "GetBook", b);
}
}
}


If you wish to support both methods then you can use Request[index] instead Request.Form[index] or Request.QueryString[index].
We will go for ModelDataBinding  in the next Post

Leave a Reply