0

I have a Partial view that contains a form ajax in Asp.net core 2.0.

You can see Partial View named _loginPartial

@model partialView.Models.LoginViewModel

<div id="login">
<form asp-controller="Account" asp-action="Login" id="frmLogin" data-ajax="true" data-ajax-method="POST" data-ajax-update="#frmLogin" data-ajax-mode="Replace">
    <div id="login">

        <div class="form-group">
            <input asp-for="UserName" class="form-control" />
            <span asp-validation-for="UserName"></span>
        </div>

        <div class="form-group">
            <input asp-for="Password" class="form-control" />
            <span asp-validation-for="UserName"></span>
        </div>

        <div class="form-group pull-left ">
            <button type="submit" class="btn btn-success">Submit</button>
        </div>
    </div>
</form>

When i submit form to /Account/Login everything is ok, but after login i want to redirect to another view in another Area

    [HttpPost]
    public async Task<IActionResult> Login(LoginViewModel model)
    {
        if (ModelState.IsValid)
        {
            //Success Login
            return Redirect("/AdminPanel/Home/Index");
        }
        else
        {
            //if ModelState is invalid

        }

        //if UserName Or Password is incorrect
        return PartialView("_loginpartialView", model);
    }

And here is LoginViewModel

public class LoginViewModel
{
    [Display(Name = "UserName")]
    [Required(AllowEmptyStrings = false, ErrorMessage = "Please Enter UserName")]
    public string UserName { get; set; }

    [Display(Name = "Password")]
    [Required(AllowEmptyStrings = false, ErrorMessage = "Please Enter Password")]
    [DataType(DataType.Password)]
    public string Password { get; set; }


}

Also i use this code to Show Partial View in index

  @Html.Partial("_loginpartialView")

But there is a problem. After success login new view replace with Partial view.In other words, it does not navigate to /AdminPanel/Home/Index and new View replace with Partial View, How can i navigate to another view in this case?

8
  • The whole point of ajax is to stay in the same page! If you want to redirect, do not use ajax. Make a normal submit. Commented Aug 11, 2018 at 13:20
  • @StephenMuecke But with normal submit cant show model errors if ModelState not valid in partial view Commented Aug 11, 2018 at 13:22
  • @StephenMuecke I disagree. This one doesn't seem an SPA so it could be ok to reload the page when there are many changes. I think a login could fall into that category. Commented Aug 11, 2018 at 13:24
  • @AdamSimon, Read the question - OP wants to redirect - it is utterly pointless (and degrading performance) to use ajax Commented Aug 11, 2018 at 13:26
  • 1
    @StephenMuecke Looking at the controller code, I'd say OP wants to redirect when the login succeed. Otherwise he wants a partial update displaying an error. Not necessarily the best way but still seems ok to me. Commented Aug 11, 2018 at 13:32

1 Answer 1

0

As you are dealing with an AJAX callback, you can't do the redirection on server-side. This must be handled on client-side using Javascript.

Examine the response from the server and if it's a redirection then set window.location to the received URL.

Sign up to request clarification or add additional context in comments.

2 Comments

You say i should use jquery ajax or javascript?
Exactly - if you want to go down the road you described.

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.