[英]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.