1

In my _Layout.cshtml I write javascript code, that allow me to display modal dialog on the page with form for user login when I click on link, and that is work. My problem is that I want do something like that:

I click on ActionLink, that run code from controller, i.e:

    public ActionResult Create()
    {
        if (Session["UserID"] != null)
        {
            ViewBag.PlaceID = new SelectList(db.Places, "PlaceID", "Name");
            ViewBag.UserID = new SelectList(db.Users, "UserID", "FullName");
            return View();
        }
        else
        {
            return RedirectToAction("LogIn", "User");
        }
    } 

Anyone have idea, how to change return RedirectToAction("LogIn", "User"); code, for something that show me my modal dialog instead of opening new page for login?

2 Answers 2

1

You should handle the click in javascript.

Your click should open the modal window, this could be a partial you return from a controller.. or just do a show hide. have the login sitting on the page already. But if you wanted to you could put a remote URL (which would be a call to your controller) in the source of the modal window.

check out this:

ASP.Net MVC jQuery Dialog Partial

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

3 Comments

My question isn't about how to load something in jquery dialog. My problem is to open this dialog if my controller finds that this needs.
Ok i see. It is possible.. but not really the way to go about it. #1 if your initial call to the controller is not done with ajax then you are making a new page request. So your page will reload. To get the modal window to open your model could have a "showModal" var passed to the view. In the view you could have some js that would run $(document).ready(function () { if(@showModal) then show it.. }); But the other way would be to make the initial call with ajax... check the response.. if you need to redirect to a new view do it in the response.. otherwise show the modal.
Thanks, this seems a good approach, I will try to implement it soon.
0

use an Ajax.ActionLink().

controller:

public ActionResult Login() 
{
    if(Request.IsAjaxRequest())
    {
        return PartialView();
    }
    return View(); //fallback for no-js
}

and in the view:

Ajax.ActionLink("Login", "Login", "Accounts", null, new AjaxOptions { UpdateTargetId = "modal-login", OnComplete = "$('#modal-login').dialog('open');" })

2 Comments

It's still not what I need. My links must go to diffrent actions in diffrent controllers, and that actions must check to open view or modal window for login.
Well, there's always just using 2 different links. @if(Request.IsAuthenticated) { // link to somewhere } else { // link to login }

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.