Ashish Bisht
Ashish Bisht

Reputation: 292

Url.Action is calling Controller but not return the view

Button event in view:

var target = '@Url.Action("Login", "Home")';
$.post(target, { UserName: $('#txtEmail').val(), userType: $('#selection').val(),   
Password: $('#txtPassword').val() });

and This is controller Action method.This controller is inside the area. I am Calling this controller from another controller action method.

    [HttpPost]
    public ActionResult Login(string Username, string userType, string password)
    {

        HealthCareDataContext dc = null;
        try
        {
            dc = new HealthCareDataContext();
            var usr = (from obj in dc.tblAdmins
                       where obj.Username == Username.Trim()
                       && obj.Status == true
                       select obj).ToList();
            if (usr.Count > 0)
            {
                FormsAuthentication.Authenticate(Username, password);  //Redirect login page.
                FormsAuthentication.RedirectFromLoginPage(Username.Trim(), false);
                return RedirectToAction("Index", "Admin", new { area1 = "Secure", area2 = "Admin" });
            }
            else
            {
                return View();
            }
        }
        catch (Exception ex)
        {
            return View();
        }
        finally
        {
            dc = null;
        }
    }

This Action method is called but it does not return the view. But when i go through the URL manually it return the view.

Where i am doing wrong with calling this action.

Upvotes: 0

Views: 1233

Answers (1)

dotnetstep
dotnetstep

Reputation: 17485

$.post(target, { UserName: $('#txtEmail').val(), userType: $('#selection').val(),   
Password: $('#txtPassword').val() }, function(result){
    //Now check your result
});

I still have doubt that you have to do this because you are using redirectoaction inside login which will create problem.

If you want to return then try pure ajax call like this.

$.ajax({
            type: 'POST',
            url: target,
            data: $.param({ UserName: $('#txtEmail').val(), userType: $('#selection').val(),   
Password: $('#txtPassword').val() }) , 
            success: function (result) {
                alert(result);
            },
            error: function (result) {
                alert('Error');
            }            
        });

Upvotes: 1

Related Questions