将数据传输到ApplicationController

时间:2011-10-20 08:53:27

标签: asp.net asp.net-mvc-3 master-pages

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

首先登录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();
    }

}

1 个答案:

答案 0 :(得分:0)

ViewData在MVC中用于将数据从Controller传递到View

和Tempdata用于将数据从一个Controller传递到其他

参考Passing State Between Action Methods

See this example for Step by step -