Open jquery modal dialog from controller in MVC3
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", "Fu开发者_如何学运维llName");
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?
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
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');" })
精彩评论