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 :)

Tanmay
  • 1,123
  • 1
  • 10
  • 20
  • Please visit this link https://www.c-sharpcorner.com/article/crud-operations-in-mvc-using-entity-framework-with-ajax-call-jquery-and-all-val/ – sebu Apr 30 '18 at 09:04
  • 1
    You can use Ajax: https://stackoverflow.com/questions/19663762/mvc-ajax-post-to-controller-action-method?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Zubayer Bin Ayub Apr 30 '18 at 09: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. – ADyson Apr 30 '18 at 09:16
  • I can't submit username and password to action in controller .It is not relevant with refreshing – Nadide YILMAZ Apr 30 '18 at 09: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)? – ADyson Apr 30 '18 at 09:55
  • I really tried to understand you .thank you for your attention.I will look to the answer it looks helpful but i was looking an answer my javascript code for sending and for account controller .I am new at this – Nadide YILMAZ Apr 30 '18 at 10:04

3 Answers3

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);
  }
arslanaybars
  • 1,813
  • 2
  • 24
  • 29
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>              

    }
Uzzal Prasad
  • 115
  • 1
  • 11
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.
user123456
  • 2,524
  • 7
  • 30
  • 57