Reputation: 144
I need to call a certain webpage based on Id
in user.RoleId
which is in my controller and in the script page it needs to call that RoleId
using session.
My Controller:
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);
My View:
<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>
Upvotes: 0
Views: 55
Reputation: 1507
You should change this part:
(data.Session["userRole"] == 4 )
to:
(data.RoleId == 4 )
BTW you controller code can be optimized.
Upvotes: 1