简体   繁体   English

登录后如何使用自己的 url 返回另一个视图,而不是在同一登录页面中加载? MVC

[英]How to return another view with its own url after login instead of loading inside same login page? MVC

I want to redirect to welcome page and it should show/hide html elements according to user type.我想重定向到欢迎页面,它应该根据用户类型显示/隐藏 html 元素。 I have written return View("Welcome", adminvar);我写了return View("Welcome", adminvar); but it returns the Welcome page inside login URL.但它会返回登录 URL 内的欢迎页面。

Upon refresh it shows popup warning of form resubmission.刷新后,它会显示重新提交表单的弹出警告。 I want to redirect to Welcome page.我想重定向到欢迎页面。 I tried this我试过这个

return RedirectToAction("Welcome" , adminvar);

but it's not working.但它不起作用。

[HttpPost]
public ActionResult Login(tbl_Admin adminObj)
{ 
        studentDBEntities db = new studentDBEntities();
        var adminvar = db.tbl_Admin.Where(x => x.Email == adminObj.Email && x.Password == adminObj.Password).Select(s=> new tbl_AdminVM {
              AdminId = s.AdminId,
               Email = s.Email,
               Name = s.Name,
               Password = s.Password,
               Type = s.Type
        }).FirstOrDefault();
        
        if (adminvar != null)
        {
            
            /* return RedirectToAction("Welcome" , adminvar);*/
            return View("Welcome", adminvar);
        }
        else 
        {
            return View();
        }
}

public ActionResult Welcome()
{
    ViewBag.Message = "Welcome Admin - Admin Account Page";
    return View();
}

View:看法:

@if (Model.Type)
{
    <center><p>@Html.ActionLink("Admin Management", "ListAdmin")</p></center>
}

Here you are returning Welcome View to the login method that will render welcome view content on login page and will not be redirected to welcome page.在这里,您将欢迎视图返回到登录方法,该方法将在登录页面上呈现欢迎视图内容,并且不会重定向到欢迎页面。

What you can do is, after successful login, redirect to Welcome Action.您可以做的是,成功登录后,重定向到 Welcome Action。

return RedirectToAction("Welcome", new { userType = adminvar.Type });

And modify Welcome action as below并修改欢迎操作如下

public ActionResult Welcome(string userType)

Inside Welcome action get the value of usertype and send it to Welcome view using Viewbag.在 Welcome 操作中获取 usertype 的值并使用 Viewbag 将其发送到 Welcome 视图。

ViewBag.userType = userType;

Use the value of ViewBag.userType on Welcome Page to show/hide html elements.使用欢迎页面上 ViewBag.userType 的值来显示/隐藏 html 元素。

You Can redirect to action您可以重定向到操作

return RedirectToAction(“ActionName”,”ControllerName”); 

or或者

return RedirectToAction(“~/ControllerName/ActionName”);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 返回登录视图,而不是在MVC 2应用程序(站点范围内)中重定向 - Return login view instead of redirecting in an MVC 2 application (site wide) 登录后如何返回上一个URL? (AJAX) - How to return to previous URL after login? (ajax) 会话在mvc4中过期后无法返回登录页面 - Unable to return to login page after session expires in mvc4 Google登录后如何重定向到另一个页面? - How to redirect to another page after Google Login? 登录成功后返回登录页面 - return to login page after login success 无法让 Identity Server 4 API 授权自己的端点,而是发送登录页面 - Can't get Identity Server 4 API to authorize its own endpoints, sends login page instead 在 Asp.net 核心 mvc 中登录后重定向到同一页面 - Redirect to same page after Login in Asp.net core mvc 登录后返回特定页面 - Return to specific page after login 如何将用户通过登录视图传递到MVC中的安全页面 - How to pass user through login view to safe page in mvc 如何将ASP.NET MVC默认登录页面更改为通过用户名而不是电子邮件地址登录? - How do I change the ASP.NET MVC default login page to login by username instead of email address?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM