0

Login.cshtml

<script type="text/javascript">
function data() {
    var n = document.getElementById("username").value;
    var m = document.getElementById("password").value;
    ?
</script>

<button  type="submit"class="btn-login" onClick="Data()">
                        Giriş Yap </button>

Account Controller

  DBEntities dB = new DBEntities();
  [HttpPost]                         (username)       (password)
    public ActionResult Login(string KullaniciAdi,string Sifre)
    {
      //  Session[KullaniciAdi] = dB.Profil.Where(x => x.KullaniciAdi == KullaniciAdi && x.Sifre == Sifre).FirstOrDefault();
        var session = (from p in dB.Profil
                       where p.KullaniciAdi == KullaniciAdi
                       select p).FirstOrDefault();

        return View(); 
    }

My OOP homework is this website and i want to create a login with sending password and username to controller and compare submitted data and the data in the database .Please help :)

6
  • Please visit this link c-sharpcorner.com/article/… Commented Apr 30, 2018 at 9:04
  • 1
    You can use Ajax: stackoverflow.com/questions/19663762/… Commented Apr 30, 2018 at 9:07
  • what's wrong with using a regular form submission? Or are you trying to do it without a page refresh (i.e. ajax)? It's not clear. Commented Apr 30, 2018 at 9:16
  • I can't submit username and password to action in controller .It is not relevant with refreshing Commented Apr 30, 2018 at 9:23
  • ".It is not relevant with refreshing" I have no idea what this means. Can you clarify? What's wrong with using a form to do this (as per the answer below)? Commented Apr 30, 2018 at 9:55

3 Answers 3

3

You can send client data by using ajax request,

this is your inputs

<input id="username" type="text" />
<input id="password" type="password" /> 
<input id="loginbutton" onclick="UserLogin()" type="submit" value="Submit" />

Submit button bind with UserLogin js function.

here is the js function. we are sending ajax post request here to our controller

  <script type="text/javascript">
    function UserLogin() {
        var n = document.getElementById("username").value;
        var p = document.getElementById("password").value;

        var postObj = JSON.stringify({
            "username": n,
            "password": p
        });

        $.ajax({
            url: "/Home/Login", // endpoint
            type: "POST",
            data: postObj,
            contentType: "application/json; charset=utf-8",
            success: function (result) {
                // success
            },
            error: function (errorData) { onError(errorData); }
        });
    }
</script>

And your controller should be like, you can implement login logic inside of the method.

  [HttpPost]
  public ActionResult Login(string username, string password)
  {
      // use your code loged in

      return Json(true, JsonRequestBehavior.AllowGet);
  }
Sign up to request clarification or add additional context in comments.

Comments

3

First You Create a Class :

public class LoginModel
    {
        [Required(ErrorMessage = "User Name can not be empty!")]
        public string LOGINID { get; set; }

        [Required(ErrorMessage = "Password can not be empty!")]
        public string LOGINPW { get; set; }      

    }

Then Your Controller Action Method do this:

     public ActionResult Login()
     {           
         return View();    
     }

    [HttpPost]                         
    public ActionResult Login(LoginModel model)
    {

       //Here check the Login Id and Password 
        return View(); 

    }

Now in view Write this. Now when you click the submit button a post call go to the Login Controller with given LOGINID and LOGINPW :

@using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)

         <!-- login screen -->  

                <form action="#">
                    @Html.TextBoxFor(m => m.LOGINID, htmlAttributes: new { @class = "form-control", placeholder = "Login ID" })
                    @Html.ValidationMessageFor(model => model.LOGINID, "", new { @class = "text-danger", style = "float: left" })

                    @Html.PasswordFor(m => m.LOGINPW, htmlAttributes: new { @class = "form-control", placeholder = "Password" })
                    @Html.ValidationMessageFor(model => model.LOGINPW, "", new { @class = "text-danger", style = "float: left" })


                    <button type="submit" class="btn btn-primary" style="background: #2e6da4; color: #FFF;">Login</button>

                </form>              

    }

Comments

0
    @{
        var actionURL = Url.Action("Action", "Controller", 
                                   FormMethod.Post, Request.Url.Scheme)  
                        + Request.Url.PathAndQuery;
    }
    @using (Html.BeginForm("Action", "Controller", FormMethod.Post, 
                                                       new { @action = actionURL }))
    {
<input type="text" name="username">
<input type="text" name="password">
<button  type="submit"class="btn-login"></button>
    }//Then use Request.Form[0] with the id to get the user name and password in the controller action.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.