Showing posts with label get. Show all posts
Showing posts with label get. Show all posts

Friday, April 3, 2015

Jquery get method in MVC 4.0

In previous blog post I had discussed about how to post form data to back end, to controller and then to database, and here in this tutorial session I will show you how to get data asynchronously to server ie controller and from there to database. So how to do this? Honestly speaking its very simple, a few steps and its done.

So lets start by creating a new application, name it as you want. Create a new controller and a view against that controller.

We are going to implement a Login form, it may be any others also. Whatever the form is functionality is same. I choose a login form to show you choose according to your requirement. 

For login form we need two text boxes and one button. So design it quickly.

<body>
    <h2>Login</h2>

    <div id="login">
        <p>
            <input type="text" id="email" /></p>
        <p>
            <input type="password" id="password" /></p>
        <p>
            <input type="button" value="Login" id="btnLogin" /></p>
    </div>
</body>

Now add the jquery method.Before do any type of jquery operation add the jquery api. Here I am using Google api. You can also use it from different other sites(like jquery.com) or you can use it as your own.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

Now we can do coding of jquery.

On the btnLogin click we will call the get method and pass the variable to controller. Before we proceed we have to create a new controller where we can catch the data we are sending via jquery. 

So create  a new controller named Login.

Now write down these jquery code into your view page.

<script>
        function login() {
            var email = document.getElementById("email").value;
            var pass = document.getElementById("password").value;

            $.get("@Url.Action("Login")",
                      {
                          email: email,
                          password: pass
                      }, function (data) {
                          if (data == "1") {
                              window.location.href = '../admin/dashboard';
                          }
                          else {
                              document.getElementById("lblMessage").innerHTML = data;
                          }
                      }
                    );
        }
    </script>

After this add a onclick properties to btnLogin. 

Now its turn for the Login controller.

public ActionResult CheckLogin(string email, string password)
{
      clsUser objUser = new clsUser();
      bool f = objUser.CheckLogin(email, password);
      if (f)
      {
           Session["useremail"] = email;
           return Content("1");
      }
      else
      {
           return Content("Please enter valid email & password !");
      }
}

Here I have created a method CheckLogin under clsUser. In that method I am actually checking whether the user is valid or not. So lets take a look on that method.

public bool CheckLogin(string email, string pass)
{
    bool f = false;

    DataTable dt = new DataTable();
    string sql = "";
    sql = "select id from tblUser where email='"+email+"' and password = '"+pass+"'";
    dt = objServer.GetDataTable(sql);
    if (dt.Rows.Count == 1)
    {
         f = true;
    }
    else
    {
         f = false;
    }
    return f;
}

Here I am using a simple ADO.NET code. Its not the right process to so. I have just used just because to make this short. I highly recommend to use parametrize query to do such thing.

Add a new connection string in web.config  to configure your server with this. And what else? your application is done. You have created it. Now test it and enjoy.

Download the full source code here.


Sunday, September 21, 2014

GET POST Method in ASP.NET C#

Many of you are aware of the GTE, POST, PUT & DELETE methods in ASP.NET, these are the basic method to send the value form one Form to another Form. Lets see how these work in ASP.NET project using C#.

GET :
Using GET method you can send all the input (textbox,textarea,radio button, checkbox etc..) to another form or another page. So how to do this?

Take a HTML page and write down the following HTML codes.

In Default.aspx to get the value of these two text boxes values we have to write code in Page_Load method. Make one thing clear, in the time of GET, POST we use NAME instead of ID. So lets check what will be for get method.
protected void Page_Load(object sender, EventArgs e)
{
     string name = ""; string pass = "";
     if (Request.QueryString["Name"] != null && Request.QueryString["Password"] != null)
     {
           name = Request.QueryString["Name"].ToString();
           pass = Request.QueryString["Password"].ToString();
     }
}

By Request.QueryString["Name"].ToString() this one we can get the value of a field which is sending through GET method. When we hit the submit button the URL will be something like this.

localhost/default.aspx?Name=arka&Password=arka1 

Normally we don't use GET to send sensitive fields like Passwords because it shows in the URL, I used it here just for an example.

POST :
Unlike GET in the POST method we can't see the values in the URL. It sends the values internally. Basically it has 2 parts. Head and body. In the head section it holds the variable name and in the body part holds the value. We will use the same example for the POST also.
Now lets check how to get the value using POST method in C# coding.
protected void Page_Load(object sender, EventArgs e)
{
     string name = ""; string pass = "";
     if (Request.Form["Name"] != null && Request.Form["Password"] != null)
     {
           name = Request.Form["Name"].ToString();
           pass = Request.Form["Password"].ToString();
     }
}
use Request.Form instead of Request.QueryString to get the value from POST method. And rest of the thing is same as GET.

Popular Posts

Pageviews