Hello All, We are going to start new batch from next week. message/call or mail us for more details.

20 February 2014

Simple Registration Page Using Asp.Net MVC 4 - DotNet Brother

Follow the step :

Lets first open your Visual studio 2012 and create an empty asp.net mvc 4 application with razor engine then Add a controller to the project named ”HomeController.cs” with below code:

using SimpleDataEntryApp.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace SimpleDataEntryApp.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        public ActionResult Index()
        {
            int hour = DateTime.Now.Hour;
            ViewBag.Time = hour < 12 ? "Good Morning!" : "Good Afternoon!";
            return View();
        }
    }
}


Now  create a model class and named “Seminar.cs” and add some properties like below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace SimpleDataEntryApp.Models
{
    public class Seminar
    {
        public string name { getset; }
        public string email { getset; }
        public string phone { getset; }
        public bool? willAttend { getset; }
    }
}
Now add a view to the project by right clicking in the Index() ActionResult in the “HomeController.cs” file and named it “Index.cshtml”.
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
       <h3> @ViewBag.Time </h3><br />
        Simple Registration Page Using Asp.Net MVC 4<br />
        Click here to insert records &nbsp;@Html.ActionLink("here","RegForm")
    </div>
</body>
</html>
Now you  have added an ActionLink in this view to move to the next page when we click on “here”.
Next you have to add  a ViewResult method in the “HomeController.cs” file like below:


[HttpGet]
public ViewResult RegForm()
{
   return View();
}
now also add a view to this viewresult and named it as ”RegForm” and make it a strongly-typed view like this:

Now lets write few code in the “RegForm.cshtml” file.

@model SimpleDataEntryApp.Models.Seminar
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>RegForm</title>
</head>
<body>
    @using (Html.BeginForm())
    {
        <p>Name : @Html.TextBoxFor(m => m.name)</p>
        <p>Email : @Html.TextBoxFor(m => m.email)</p>
        <p>Phone : @Html.TextBoxFor(m => m.phone)</p>
        <p>
            Will you attend? : @Html.DropDownListFor(m => m.willAttend, new[]{
                             new SelectListItem() {Text = "Yes", Value = bool.TrueString},
                             new SelectListItem() {Text= "No", Value= bool.FalseString},
                         },"Choose an option")
        </p>
        <input type="submit" value="Submit" />
    }
</body>
</html>


so in this view, we have created a simple form with three textboxes and a dropdownlist and a submit button to submit the values.
Now we have created a form, next we need a page to display the values which user has inputted in the form, so add another viewresult to the controller that is “HomeController.cs” like this:


[HttpPost]
public ViewResult RegForm(Seminar seminarResponse)
{
   return View("Thanks", seminarResponse);
}
So the whole “HomeController.cs” file look like this:
using SimpleDataEntryApp.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace SimpleDataEntryApp.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        public ActionResult Index()
        {
            int hour = DateTime.Now.Hour;
            ViewBag.Time = hour < 12 ? "Good Morning!" : "Good Afternoon!";
            return View();
        }
        [HttpGet]
        public ViewResult RegForm()
        {
            return View();
        }
        [HttpPost]
        public ViewResult RegForm(Seminar seminarResponse)
        {
            return View("Thanks", seminarResponse);
        }
    }
}


Now add a view to the viewresult RegForm(Seminar seminarResponse) by right clicking to it named “Thanks.cshtml” and make it a strongly-typed view like this:
 
Now write the below code in it:
@model SimpleDataEntryApp.Models.Seminar
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Thanks</title>
</head>
<body>
    <div>
        <h1>Thank you, @Model.name</h1>
        @if (Model.willAttend == true)
        {
            @: OK
        }
        else
        {
            @:Sorry:(
        }
    </div>
</body>
</html>


Output
Now run the application:




















When you click on “here” will open the “RegForm.cshtml”


Fill the form with appropriate values and click on submit button:





1 comment:

  1. Great !!! have never seen such a beautiful description. Thank you so much . Rajeev

    ReplyDelete