将数据传输到应用程序控制器

发布于 2024-12-11 08:37:49 字数 4509 浏览 0 评论 0原文

我正在尝试使用视图母版页创建登录模块。首先用户通过登录表单访问主页,当用户单击登录时,页面应首先重定向到 UserLoginController,然后重定向到另一个包含具有相同母版页的所有页面的 PanelController。我想根据不同用户的权限显示不同的菜单。正如我参考一篇文章 http://www.asp.net/mvc/tutorials/passing-data-to-view-master-pages-cs" rel="nofollow">http://www. asp.net/mvc/tutorials/passing-data-to-view-master-pages-cs 我创建了一个抽象类ApplicationController,PanelController继承它。在构造函数中,我想获取登录用户的信息来识别用户的权限,但似乎没有Request和Session。请参阅代码。

首先登录Javascript

    <script type="text/javascript" language="javascript">
    $(document).ready(function () {
        $(btnLogin).click(function () {
            var sso = $(txtSSO).val(); 
            var pwd = $(txtPwd).val(); 
            if (sso == "")
            { alert("Please input your SSO number"); }
            else if (pwd == "")
            { alert("Please input your password"); }
            else {
                jQuery.ajax(
                { url: '<%:Url.Action("UserLogin", "UserLogin")%>',
                    data: { sso: sso, pwd: pwd },
                    success: function (data) {
                        window.location = '<%: Url.Action("Demo","Panel") %>';
                    }
                }
                );
            }
        });
    });

</script>

UserLoginController

 public ActionResult UserLogin()
    {
        string sso = "";
        string pwd = "";
        try
        {

            if (Request.IsAjaxRequest())
            {
                sso = Request.Params["sso"].ToString();
                pwd = Request.Params["pwd"].ToString();
            }

            Regex reg = new Regex("^[0-9]{9}$");
            if (!reg.Match(sso).Success || pwd == "")
            {
                ViewData["errorMsg"] = "Either your UserID or your Password is incorrect";
                return View("Index");
            }
            SystemAdminEntities entity = new SystemAdminEntities();
            var result = entity.ValidateUserLogin(sso, pwd).FirstOrDefault();

            if (result == 1)//User is found
            {
                int isso = Convert.ToInt32(sso);
                var dbEmp = (from e in entity.sys_employee
                             where e.sso == isso
                             select e);
                SysEmployee emp = dbEmp.FirstOrDefault<SysEmployee>();
                LogonUserModel currentUser = LogonUserModel.GetUser();
                currentUser.CopyUserInfo(emp);

                //FormsAuthenticationTicket ticket=new
                FormsAuthentication.SetAuthCookie(currentUser.SSO.ToString(), true);
                Session.Add("emp", currentUser);
                this.Session.Add("sso", currentUser.SSO);
                this.Session.Add("empid", currentUser.EmpID);
                this.Session.Add("ename", currentUser.EName);
                return RedirectToAction("Demo", "Panel");//重定向到 Demo
            }
            else if (result == 0)//User is not found
            {
                ViewData["errorMsg"] = "User isn't found";
                return View("Index");
            }
            else if (result == 2)//Password not correct
            {
                ViewData["errorMsg"] = "Password Error";
                return View("Index");
            }
            return View("Index");
        }
        catch { return View("Index"); }
    }

ApplicationController

  public abstract class ApplicationController : Controller
{
    private SystemAdminEntities _entities = new SystemAdminEntities();

    public ApplicationController()
    {
        //根据人员判断权限
        int sso = 0;//= Request.Form["sso"].ToString();
        try
        {
            sso = int.Parse(Session["sso"].ToString());
            var e = (from emp in _entities.sys_employee//得到对应的用户
                     where emp.sso == sso
                     select emp
                );
            SysEmployee loginUser = e.FirstOrDefault<SysEmployee>();
            ViewData["modules"] = loginUser.SysHasPerm;
        }
        catch
        {
            ViewData["modules"] = null;

        }

    }

PanelController

 public class PanelController : ApplicationController
{

    //
    // GET: /Panel/

    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Demo()
    {
        return View();
    }

}

I'm trying to do a login module with view master page. First user access to a home page with a login form, when user click login, page should redirect to a UserLoginController first and then redirect to another PanelController which holds all pages with same master page. I want to show different menus by different user's permission. As I refer a article http://www.asp.net/mvc/tutorials/passing-data-to-view-master-pages-cs I create a abstract class ApplicationController, the PanelController inherit it. In the constructor, I want to get the login user's information to identify user's permission, but it seems Request and Session is not available. Pls see code.

First the login Javascript

    <script type="text/javascript" language="javascript">
    $(document).ready(function () {
        $(btnLogin).click(function () {
            var sso = $(txtSSO).val(); 
            var pwd = $(txtPwd).val(); 
            if (sso == "")
            { alert("Please input your SSO number"); }
            else if (pwd == "")
            { alert("Please input your password"); }
            else {
                jQuery.ajax(
                { url: '<%:Url.Action("UserLogin", "UserLogin")%>',
                    data: { sso: sso, pwd: pwd },
                    success: function (data) {
                        window.location = '<%: Url.Action("Demo","Panel") %>';
                    }
                }
                );
            }
        });
    });

</script>

The UserLoginController

 public ActionResult UserLogin()
    {
        string sso = "";
        string pwd = "";
        try
        {

            if (Request.IsAjaxRequest())
            {
                sso = Request.Params["sso"].ToString();
                pwd = Request.Params["pwd"].ToString();
            }

            Regex reg = new Regex("^[0-9]{9}$");
            if (!reg.Match(sso).Success || pwd == "")
            {
                ViewData["errorMsg"] = "Either your UserID or your Password is incorrect";
                return View("Index");
            }
            SystemAdminEntities entity = new SystemAdminEntities();
            var result = entity.ValidateUserLogin(sso, pwd).FirstOrDefault();

            if (result == 1)//User is found
            {
                int isso = Convert.ToInt32(sso);
                var dbEmp = (from e in entity.sys_employee
                             where e.sso == isso
                             select e);
                SysEmployee emp = dbEmp.FirstOrDefault<SysEmployee>();
                LogonUserModel currentUser = LogonUserModel.GetUser();
                currentUser.CopyUserInfo(emp);

                //FormsAuthenticationTicket ticket=new
                FormsAuthentication.SetAuthCookie(currentUser.SSO.ToString(), true);
                Session.Add("emp", currentUser);
                this.Session.Add("sso", currentUser.SSO);
                this.Session.Add("empid", currentUser.EmpID);
                this.Session.Add("ename", currentUser.EName);
                return RedirectToAction("Demo", "Panel");//重定向到 Demo
            }
            else if (result == 0)//User is not found
            {
                ViewData["errorMsg"] = "User isn't found";
                return View("Index");
            }
            else if (result == 2)//Password not correct
            {
                ViewData["errorMsg"] = "Password Error";
                return View("Index");
            }
            return View("Index");
        }
        catch { return View("Index"); }
    }

The ApplicationController

  public abstract class ApplicationController : Controller
{
    private SystemAdminEntities _entities = new SystemAdminEntities();

    public ApplicationController()
    {
        //根据人员判断权限
        int sso = 0;//= Request.Form["sso"].ToString();
        try
        {
            sso = int.Parse(Session["sso"].ToString());
            var e = (from emp in _entities.sys_employee//得到对应的用户
                     where emp.sso == sso
                     select emp
                );
            SysEmployee loginUser = e.FirstOrDefault<SysEmployee>();
            ViewData["modules"] = loginUser.SysHasPerm;
        }
        catch
        {
            ViewData["modules"] = null;

        }

    }

The PanelController

 public class PanelController : ApplicationController
{

    //
    // GET: /Panel/

    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Demo()
    {
        return View();
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流年已逝 2024-12-18 08:37:49

ViewData 用于在 MVC 中将数据从控制器传递到视图

,而 Tempdata 用于将数据从一个控制器传递到另一个控制器

请参阅 在操作方法之间传递状态

请参阅此示例了解分步操作 -

ViewData is used in MVC to pass data from Controllor to View

and Tempdata is used to pass data from one Controllor to other

Refer Passing State Between Action Methods

See this example for Step by step -

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文