Showing posts with label Post. Show all posts
Showing posts with label Post. Show all posts

Feb 19, 2015

Postback Vs Callback in ASP.NET

ASP.NET was introduced with a mechanism to post an HTTP POST request back to the same page. It’s basically posting a complete page back to server (i.e. sending all of its data) on same page. So, the whole page is refreshed.In order to understand how this postback mechanism works in ASP.NET, follow the simple steps:
  • Add a new ASP.NET web form page to a project e.g. WebForm1.aspx.
  • View the page code in HTML Source view. You will find something like following screen.

  • Look the form line of code.
    <form id=”form1″ runat=”server”>
    It represents a server-side implementation of form control.
  • Now just run the application to see WebForm1.aspx page and view its source code. HTML source of the page will display form element as follows:
    <form method=”post” action=”WebForm1.aspx” id=”form1″>

    You can see that an HTML form element generated with an HTTP method as “POST” and action=”WebForm1.aspx”. So, if a submit button is clicked, the page will postback to itself by default.Following figure will also give you more understanding on ASP.NET Postback.
callback is generally a call for execution of a function after another function has completed.”
But if we try to differentiate it from a postback then we can say: It’s a call made to the server to receive specific data instead of whole page refresh like a postback. In ASP.NET, its achieved using AJAX, that makes a call to server and updating a part of the page with specific data received.

May 10, 2013

Creating the Registration Form in ASP.NET MVC 3 (Get and Post) Using Razor View

 
Introduction
In MVC (Model View Controllers) 3 I used a Razor view to create a simple registration form. To do this, first the registration request page is opened; if the user clicks on the registration link then the registration form is opened and after entering all the details the congratulation message will displayed that shows the registration is successfully completed. In this I used models, views and controllers. To create this simple form use the following steps:

Step 1 : Open the Visual Studio 2010 and select new project from the file menu. A dialog box appears; in that, under C# -> web, select "ASP.NET MVC3 Application" and click the Ok button:

razor1.gif

Step 2 : A window is opened; in that select Empty from the template option and select Razor from the View engine option and click ok.

razor2.gif

Step 3 : Now add the Controller by right-clicking on the controller folder placed at the Solution Explorer by clicking add->Controller.

razor3.gif

Step 4 : A window will opened; give the name of the controller as HomeController and add it:

razor4.gif

Step 5 : Modify the HomeController.cs file as:
using System.Web.Mvc;

namespace MvcApplication4.Controllers
{
    public class HomeController : Controller
    {
        public ViewResult Index()
        {
            return View();
        }
    }
}

Step 6 : Now right-click on the ViewResult method in order to add the view, like:

razor5.gif

Step 7 : A window will appear; in it give the name of the view and click add. The extension of the view is cshtml.

razor6.gif

Step 8 : Write the code in index.cshtml as:
 
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <title>Index</title>
</head>
<body>
    <div>
        <h3>Please fill the registration form by clicking on the following registration link</h3>
    </div>
</body>
</html>
Step 9 : Add the Model by right-clicking on the Model folder in the Solution Explorer and add the class:

razor7.gif
Step 10 : A window will appear; in it select a class and give the name of the class as Registration.cs and click ok:

razor8.gif
Step 11 : Write the code in Registration.cs as:
 
namespace MvcApplication4.Models
{
    public class Registration
    {
        public string Name { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
        public string Address { get; set; }
    }
}

Step 12 : Now in order to add the registration link modify the index.cshtml file as:
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <title>Index</title>
</head>
<body>
    <div>
        <h3>Please fill the registration form by clicking on the following registration link</h3>
        @Html.ActionLink("Registration", "RegistrationForm")
    </div>
</body>
</html>
Step 13 : When I run this the output will look like:
razor9.gif
Step 14 : Now In order to perform the action when I click on the Registration link write and modify the code as, modify the Controller as:
 
namespace MvcApplication4.Controllers
{
    public class HomeController : Controller
    {
        public ViewResult Index()
        {
            return View();
        }
        public ViewResult RegistrationForm()
        {
            return View();
        }
    }
}
Step 15 : Now add the View for the Registration Form by right-clicking on the ViewResult. In this step I am going to create the strongly typed view.
razor10.gif
Step 16 : Write the code in the RegistrationForm.cshtml file as:
 
@model MvcApplication4.Models.Registration
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <title>RsvpForm</title>
</head>
<body>
    @using (Html.BeginForm())
    {
        <p>
            Your name: @Html.TextBoxFor(x => x.Name)
        </p>
        <p>
            Your email: @Html.TextBoxFor(x => x.Email)</p>
        <p>
            Your phone: @Html.TextBoxFor(x => x.Phone)</p>
        <p>
           Your address: @Html.TextBoxFor(x=>x.Address)</p>       
        <input type="submit" value="Register Me" />
    }
</body>
</html>
Step 17 : Again modify the Controller in order to generate the event when the registration button is clicked:
 
using MvcApplication4.Models;

namespace MvcApplication4.Controllers
{
    public class HomeController : Controller
    {
        public ViewResult Index()
        {
            return View();
        }
        [HttpGet]
        public ViewResult RegistrationForm()
        {
            return View();
        }
        [HttpPost]
        public ViewResult RegistrationForm(Registration registration)
        {           
            return View("Congratualation", registration);
        }
    }
}

Step 18 : Again create the view for the third ViewResult as we create for the second the strongly typed view and give the name of the view as Congratualation.

Write the code in Congratualation.cshtml file as:
 
@model MvcApplication4.Models.Registration

@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <title>Congratualation</title>
</head>
<body>
    <div>
        <h1>Congratualation, @Model.Name!!!</h1>
        <p>Your Registration has been Successfully completed.</p>
    </div>
</body>
</html>

Step 19 : Now build the application and then run it; you will see the result as:

razor9.gif

When I click on the Registration link then:

razor12.gif

When the Register Me button is clicked then:

razor13.gif
Questions came while doing this simple application  those are
What is the difference between Html.Textbox and Html.TextboxFor?
Ultimately they both produce the same HTML but Html.TextBoxFor() is strongly typed where as Html.TextBox isn't.
1:  @Html.TextBox("Name")
2:  Html.TextBoxFor(m => m.Name)will both produce
<input id="Name" name="Name" type="textbox" />