Introduction: In
this program we will learn to create a registration form used ASP.NET MVC
tools. MVC is an integrated module and this is built on three separated
words Models,Views,Controllers. Models provide the business logic views provide the presentation and controllers handle the request. Now in this
ASP.NET MVC application we will create a registration form first we
add the class in the models folder. Second we add a controller in the controller
folder and last we create the three views named ndex, create and details.
The ASP.NET MVC framework is a lightweight highly testable presentation
framework that is integrated with existing ASP.NET features such as master
pages and membership based authentication. The MVC framework is defined in
the System.Web.Mvc namespace and is a fundamental supported part of the
System.Web namespace.
Step1:
Open Visual Studio 2010.
- Go to file -> New->Projects.
- Create an ASP.NET MVC 2 Empty Web
Application.
- Name of "manishRegistration".
Step2:
Add a class in the model folder.
- Right click on the Models folder ->add new
items->add class.
- Name of Class is "manish".
- In a class define the properties.
Step3: Define the business logic in a class.
Code:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
namespace
MnaishRegistration.Models
{
public class
Mnaish
{
public string
Email_Id { get; set;
}
public string
passward { get; set;
}
public string
BirthYear { get; set;
}
public string
IndianZipCode { get;
set; }
public string
Gender { get; set;
}
}
}
Step4:
Add a controller.
- Right click on the Controllers folder
->add->Controllers.
- Name of Controllers is "manu".
- In a controller, define the request.
Step5: Write the code in controller.
Code:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.Mvc;
using
MnaishRegistration.Models;
namespace
MnaishRegistration.Controllers
{
public class
manuController :
Controller
{
static
List<Mnaish> mm =
new List<Mnaish>();
public
ActionResult Index()
{
return View(mm);
}
public
ActionResult Details(Mnaish mm)
{
return View(mm);
}
public
ActionResult Create()
{
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public
ActionResult Create(Mnaish monu)
{
if (!ModelState.IsValid)
{
return View("Create",monu);
}
mm.Add(monu);
return RedirectToAction("Index");
}
}
}
Step6: Add the 3 view.
- The first view is "index".
Step7: Write the code in index view.
Code:
<%@
Page Language="C#"
Inherits="System.Web.Mvc.ViewPage<IEnumerable<MnaishRegistration.Models.Mnaish>>"
%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html
xmlns="http://www.w3.org/1999/xhtml"
>
<head
runat="server">
<title>Index</title>
</head>
<body
bgcolor="#ff9999">
<h2
style="background-color:
#FF8080">manish application</h2>
<div
style="background-color:
#CC3399">
<table>
<tr>
<th></th>
<th>
Email
</th>
<th>
| Passward
</th>
<th>
BitrhYear
</th>
<th>
INdZipCode
</th>
<th>
Gender
</th>
</tr>
<%
foreach(var
Man in Model) {
%>
<tr>
<td>
<%=
Html.ActionLink("Details",
"Details", Man)%>
</td>
<td>
<%=
Html.Encode(Man.Email_Id) %>
</td>
<td>
<%=
Html.Encode(Man.passward) %>
</td>
<td>
<%=
Html.Encode(Man.BirthYear) %>
</td>
<td>
<%=
Html.Encode(Man.IndianZipCode) %>
</td>
<td>
<%=
Html.Encode(Man.Gender) %>
</td>
</tr>
<%
} %>
</table>
<p>
<%= Html.ActionLink("Login
Now", "Create")
%>
</p>
</div>
</body>
</html>
Step8: Add the second view.
- the second view is" create".
Step9: Write the code in create view.
Code:
<%@
Page Language="C#"
Inherits="System.Web.Mvc.ViewPage<MnaishRegistration.Models.Mnaish>"
%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html
xmlns="http://www.w3.org/1999/xhtml"
>
<head
runat="server">
<title>Create</title>
</head>
<body
bgcolor="#ffccff">
<%=
Html.ValidationSummary("Create was unsuccessful.
Please correct the errors and try again.")
%>
<%
using (Html.BeginForm()) {%>
<fieldset>
<legend>Fields</legend>
<p>
<label
for="Email_Id">Email_Id:</label>
<%=
Html.TextBox("Email_Id")%>
<%=
Html.ValidationMessage("Email_Id",
"*")%>
</p>
<p>
<label
for="Passward">Passward:</label>
<%=Html.Password("*")
%>
<%=
Html.ValidationMessage("Passward",
"*") %>
</p>
<p>
<label
for="BirthYear">BirthYear:</label>
<%=
Html.TextBox("BirthYear")%>
<%=
Html.ValidationMessage("BirthYear",
"*")%>
</p>
<p>
<label
for="IndianZipCode ">IndianZipCode
:</label>
<%=
Html.TextBox("IndianZipCode ")%>
<%=
Html.ValidationMessage("IndianZipCode ",
"*")%>
</p>
<p>
<label
for="Gender ">Gender:</label>
<%=
Html.TextBox("Gender ")%>
<%=
Html.ValidationMessage("Gender ",
"*")%>
</p>
<p>
<input
type="submit"
value="Login"
/>
</p>
</fieldset>
<%
} %>
<div>
<%:
Html.ActionLink("Back to List",
"Index") %>
</div>
</body>
</html>
Step10: Add the third view.
- the third view is" detail"
Step11: Write the code in Details view.
Code:
<%@
Page Language="C#"
Inherits="System.Web.Mvc.ViewPage<MnaishRegistration.Models.Mnaish>"
%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html
xmlns="http://www.w3.org/1999/xhtml"
>
<head
runat="server">
<title>Details</title>
</head>
<body
bgcolor="#ffff99">
<fieldset>
<legend>Fields</legend>
<div
class="display-label">Email_Id</div>
<div
class="display-field"><%:
Model.Email_Id %></div>
<div
class="display-label">passward</div>
<div
class="display-field"><%:
Model.passward %></div>
<div
class="display-label">BirthYear</div>
<div
class="display-field"><%:
Model.BirthYear %></div>
<div
class="display-label">IndianZipCode</div>
<div
class="display-field"><%:
Model.IndianZipCode %></div>
<div
class="display-label">Gender</div>
<div
class="display-field"><%:
Model.Gender %></div>
</fieldset>
<p>
<%:
Html.ActionLink("Edit",
"Edit", new
{ /* id=Model.PrimaryKey */ })
%> |
<%:
Html.ActionLink("Back to List",
"Index") %>
</p>
</body>
</html>
Step 12: press crtl+f5 run the application.
Output: