K
K
Kcid132014-07-20 22:20:50
ASP.NET
Kcid13, 2014-07-20 22:20:50

How to implement modal window on ASP.NET MVC4?

There is a main view, it has an Ajax link that loads a partial view - a modal window with a login form. If the data fails validation -- the action returns a partial view with the model, an error message is shown. If the data passes the check, the user must be redirected to another action. Instead, the view is loaded into a markup element on the same page.
How to customize the return values ​​so that everything is as it should be?

public PartialViewResult Login()
{
    return PartialView();
}
[HttpPost]
public ActionResult Login(LoginModel model)
{
    if (!ModelState.IsValid)
    {                
        return PartialView(model);
    }
    return RedirectToAction("Index", "Home");
}

Update:
Instead of RedirectToAction("Index","Home") I return a view with a js script that redirects to the page I need.
<script type="text/javascript">
    window.location = "Home/Index";
</script>

However, I still think there is a better way to do it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav Kilin, 2014-08-15
@Kcid13

Try returning ContentResult(Url.Action("Index", "Home")) and manually update location.href on request success.
I also strongly recommend that you do not write page urls on the views by hand, for this there is UrlHelper in MVC. It is enough to write
window.location = " @Url .Action("Index", "Home")"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question