dotnettraine: send data from mvc to web-api and vice versa

Search This Blog

Wednesday 3 December 2014

send data from mvc to web-api and vice versa

View in mvc

@model Watch.Models.Register
@{
    ViewBag.Title = "Login";
}
<div class="row">
    <div class="col-lg-12">
        <section id="loginForm">
            @using (Html.BeginForm())
            {
                <div class="col-md-3 " style="margin-left:0%;margin-top:0%; z-index:1">
                    <h2 class="col-md-12">@ViewBag.Title</h2>
                    @Html.LabelFor(m => m.UserAccount, new { @class= "col-md-6"})
                    @Html.TextBoxFor(m => m.UserAccount, new { @class = "col-md-6" })
                    @Html.LabelFor(m => m.Password, new { @class = "col-md-6" })
                    @Html.PasswordFor(m => m.Password, new { @class = "col-md-6" })
                    <a href="/ForgetPassword.html" class="col-md-12">Forget Password?</a>
                    <input type="submit" class="btn btn-primary form-control" />
                </div>
            }
            </section>
        </div>
    </div>




Controller getting data from the view  in mvc
[HttpPost]
        public  ActionResult Index(Register r)
        {
            var client = new HttpClient { BaseAddress = new Uri(" http://localhost:1987/") };
            //calling api
            var response = client.GetAsync("/api/AppLogin?UserText=" + r.UserAccount + "&PassText="+ r.Password).Result;
            if (!response.IsSuccessStatusCode)
                Response.Write("<script>alert('Error in Calling Web Api');</script>");
            var result = response.Content.ReadAsStringAsync().Result;
            string re = result.Replace("\"", "");
            if (re == "Success")
                Response.Redirect("/home/index");
            else
                return View();
            return View();
        }

Reading data from the mvc


public string Get(string UserText, string PassText)
        {
            Register r = new Register();
            Login l = new Login();
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("select count(*) from RegisterTbl where UserAccount='" + UserText + "' and Password='" + PassText + "'",con);
                int i = Convert.ToInt32(cmd.ExecuteScalar());
                con.Close();
                if (i != 0)
                {
                    l.UserAccount = UserText;
                    l.TimeStamp = DateTime.Now.ToString();
                    l.Data = "UserData";
                    l.Token = "yes";
                    l.Device = "Android";
                    l.msg = "Success";
                    l.Status = "0";
                    con.Open();
                    SqlCommand insertcmd = new SqlCommand("insert into Logintbl values('" + l.UserAccount + "','" + l.TimeStamp + "','" + l.Data + "','" + l.Token + "','" + l.Device + "','" + l.Status + "','" + l.msg + "')", con);
                    insertcmd.ExecuteNonQuery();
                    con.Close();

                }
                else
                {

                    l.UserAccount = UserText;
                    l.TimeStamp = DateTime.Now.ToString();
                    l.Data = "UserData";
                    l.Token = "yes";
                    l.Device = "Android";
                    l.msg = "Logged In failed";
                    l.Status = "1";
                    return "failed";
                }
            }
            catch (Exception ex)
            {
                l.UserAccount = UserText;
                l.TimeStamp = DateTime.Now.ToString();
                l.Data = "UserData";
                l.Token = "yes";
                l.Device = "Android";
                l.msg = "Logged In Successfully";
                l.Status = "1";
                string error = ex.Message;
   
            }
           
            return "Success";

        }

No comments:

Post a Comment