微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何根据Controller中的RoleId调用ajax内部的Session?

如何解决如何根据Controller中的RoleId调用ajax内部的Session?

我需要根据 Id 中的 user.RoleId 调用某个网页,该网页位于我的控制器和脚本页面中,它需要使用会话调用RoleId

我的控制器:

if (ModelState.IsValid)
            {
                var user = db.UserInfoes.ToList().Where(x => x.UserName == input.name && x.Password == input.password).FirstOrDefault();
                var casemanagers = db.CaseRegistrations.ToList();
                

                if (user != null)
                {
                    var CategoryId = db.Agencies.Where(x => x.AgencyId == user.AgencyId).Select(x => x.CategoryId).FirstOrDefault();
                    var CategoryName = db.AgencyCategories.Where(x => x.Id == CategoryId).Select(x => x.CategoryName).FirstOrDefault();
                    Session["username"] = user.UserName;
                    Session["userRole"] = user.RoleId.ToString(); ---> This is user.RoleId mentioned
                    Session["userAgency"] = user.AgencyId;
                    Session["userId"] = user.UserId;
                    Session["CategoryId"] = CategoryId;
                    Session["CategoryName"] = CategoryName;
                }
                else
                {
                    return Json("Invalid Credential");
                }
                if (Session["userRole"] != null)
                {
                    
                    return Json(user);
                }
              
            }
            return Json(status);

我的观点:

<script>
        $(document).ready(function () {
            $('#btnSubmit').click(function () {   ---> This is where it needs to be called upon onclick
                var username,password;
                username = $('#txtusername').val();
                password = $('#txtpassword').val();
                if (username == '') { $('#txtusername').addClass('error'); return; }
                else { $('#txtusername').removeClass('error'); }
                if (password == '') { $('#txtpassword').addClass('error'); return; }
                else { $('#txtpassword').removeClass('error'); }
                var input = {};
                input.name = username;
                input.password = password;
                $('#cover-spin').show(0);
                $.ajax({
                    url: '@Url.Action("GetUserInfo","Login")',type: "POST",data: JSON.stringify(input),dataType: "json",contentType: "application/json; charset=utf-8",success: function (data) {
                        if (data.Session["userRole"] == 4 ) {
                                window.location.href = '@Url.Action("Details","CaseRegistration")';
                                $('#cover-spin').hide(0);
                            }
                            else {
                            location.href = '/Dashboard/Index';
                                $('#cover-spin').hide(0);x
                                alert(data);
                            }
                        },error: function () {
                        alert("An error has occured!!!");
                        }
            })
            })
        })
    </script>

解决方法

你应该改变这部分:

 public class Program
{
    public static void Main()
    {
        Product product= new Product()
        {
            Property1=1,Property2=2,ID=3
        };
        
        Product copyProduct=new Product(product);
    }
}

public class Product
{
    public int Property1 {get;set;}
    public int Property2 {get;set;}
    public int ID {get;set;}
    
    public Product()
    {
    }
    
    public Product(Product product)
    {
        Property1=product.Property1;
        Property2=product.Property2;
    }
}

到:

(data.Session["userRole"] == 4 ) 

顺便说一句,您可以优化控制器代码。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。