Michał Turczyn
Michał Turczyn

Reputation: 37430

Calling controller, but not redirecting to another view immediately

I am working with ASP.NET Core.

I wanted to add simple login functionality to my application.

So I have form to fill password and login and button to submit data.

When submitting data, I create HTTP POST request, which calls appropriate method in my controller. The result of a controller is another view, to which user is redirected (based on login result).

What I want to do is: call controller just to check if login was successfull, based on that result, without redirecting to other page, I want to show some pop-up about success of a login, and then redirect to appropriate view.

What I can think of is to call controller two times: first for login vaildation and second for redirecting. Is this right way? Or is there any better approach?

Another way I thought of is to create HTTP request with JavaScript after showing pop-up.

Upvotes: 1

Views: 716

Answers (2)

Rahul Sharma
Rahul Sharma

Reputation: 8311

Regarding your scenario, the best way would be to use AJAX to send your form data to the Controller method and based on the response from your Controller method, perform the appropriate action on your View. A very simple example would be:

Example View:

  <table class="table table-striped">    
    <tr>
      <td>Username: </td>
      <td><input type="text" class="form-control" id="username" required></td>
    </tr>
    <tr>
      <td>Password: </td>
      <td><input type="password" class="form-control" id="userpassword" required></td>
    </tr>
  </table>
  <div class="box-footer">
    <button type="button" class="btn btn-primary" onclick="AuthenticateUser()">Submit</button>
  </div>

<script>
function AuthenticateUser() {
  var username= $("#username").val();
  var password= $("#userpassword").val();

  var json = {
          username: username,
          password: password,
         };

  $.ajax({
      type: "post",
      dataType: "json",
      data: {"json": JSON.stringify(json)},,
      url: "@Url.Action("AuthenticateUser","Home")",
      success: function (data) {
          if(data.status=="true")
          {
            var urlToRedirect= '@Url.Action("RedirectMethod","Home")';
            window.location.href = urlToRedirect; //Redirect here
          }
          else if(data.status=="false")
          {
            alert(data.msg)
          }
      },
      error:function(err){
          console.log(err);
      }            
  });
}
</script>

Example Controller method:

using System.Web.Script.Serialization;

[HttpPost]
public ActionResult AuthenticateUser(string json)
{

  var serializer = new JavaScriptSerializer();
  dynamic jsondata = serializer.Deserialize(json, typeof(object));

  //Get your variables here from AJAX call
  var username= jsondata["username"];
  var password= jsondata["password"];

  bool result=db.Authenticate(username,password) //Can be API call or DB call 

  if(bool)
  {
   return Json(new {status="true", msg= "successful authentication"}, JsonRequestBehavior.AllowGet);
  }
  else
  {
   return Json(new {status="false", msg= "Incorrect credentials given"}, JsonRequestBehavior.AllowGet);
  }

}

Hope this helps you out.

Upvotes: 2

Sandeep Sharma
Sandeep Sharma

Reputation: 1

You can try returning a boolean value by your controller method based on successful/unsuccessful login by the user. Then you can use this result for showing the pop-up, And then redirect user from that pop-up. Creating HTTP request from JavaScript is also a good option.

Upvotes: 0

Related Questions