I work on asp.net mvc . i face issue when pass user role and User code
from IndexResignation action to PendingManagersRequests action using
session because it share values between browsers .
so How to use another way instead of session as hidden field .
C#
public ActionResult IndexResignation(string filenumber)
{
int employeeFileNo;
string userRole;
if (!string.IsNullOrEmpty(filenumber))
{
if (int.TryParse(filenumber, out employeeFileNo))
{
JDEUtility jde = new JDEUtility();
userRole = Workforce.ResignationGetUserRole(employeeFileNo);
Session[SessionKeys.UserCode] = employeeFileNo;
if (!string.IsNullOrEmpty(userRole))
{
Session[SessionKeys.RoleCode] = userRole;
}
if (userRole == RoleKey.LM || userRole == RoleKey.DM || userRole == RoleKey.LDM)
{
return RedirectToAction("PendingManagersRequests", "Resignation", null);
}
}
else
{
ViewBag.errorMsg = "incorrect file no";
}
}
else
{
ViewBag.errorMsg = "unauthorized user";
}
return View();
}
public async Task PendingManagersRequests(string msg, string errorMsg)
{
// how to pass user role and user code to action PendingManager
}
can you show me view and action what will be if i use hidden fields ?
Index resignation action not have view
PendingMangerRequest action have view

Naimish MakwanaRank #324 · 7.7K reputationPosted Jan 9, 2024, 11:42 PM EST
Sure, you can use hidden fields to pass data from one action to another. Here’s how you can modify your
IndexResignationaction andPendingManagersRequestsaction:public ActionResult IndexResignation(string filenumber) { int employeeFileNo; string userRole; if (!string.IsNullOrEmpty(filenumber)) { if (int.TryParse(filenumber, out employeeFileNo)) { JDEUtility jde = new JDEUtility(); userRole = Workforce.ResignationGetUserRole(employeeFileNo); if (userRole == RoleKey.LM || userRole == RoleKey.DM || userRole == RoleKey.LDM) { return RedirectToAction("PendingManagersRequests", "Resignation", new { userCode = employeeFileNo, roleCode = userRole }); } } else { ViewBag.errorMsg = "incorrect file no"; } } else { ViewBag.errorMsg = "unauthorized user"; } return View(); } public async Task PendingManagersRequests(string userCode, string roleCode, string msg, string errorMsg) { // Now you can use userCode and roleCode in this action }In the
IndexResignationaction, I’ve replaced the session variables with parameters in theRedirectToActionmethod. This will pass theuserCodeandroleCodeto thePendingManagersRequestsaction.In the
PendingManagersRequestsaction, I’ve added parameters foruserCodeandroleCode. Now you can use these parameters in this action12.For the view of
PendingManagersRequests, you can use hidden fields to store these values if you need them for a form post:@using (Html.BeginForm("YourPostAction", "YourController", FormMethod.Post)) { @Html.Hidden("userCode", Model.userCode) @Html.Hidden("roleCode", Model.roleCode) }In this form,
userCodeandroleCodeare stored in hidden fields. When the form is posted, these values will be sent to the server12.Thanks
Jayraj ChhayaRank #304 · 8.2K reputationPosted Jan 10, 2024, 4:09 AM EST
After reviewing the provided code, I have identified a potential bug in the
IndexResignationaction. The bug occurs when theuserRoleis not equal toRoleKey.LM,RoleKey.DM, orRoleKey.LDM. In this case, the action does not return a view, which may result in unexpected behavior.To fix this bug, you can add a default return statement at the end of the
IndexResignationaction to handle the case when theuserRoleis not one of the specified values. Here's the updated code:public ActionResult IndexResignation(string filenumber) { int employeeFileNo; string userRole; if (!string.IsNullOrEmpty(filenumber)) { if (int.TryParse(filenumber, out employeeFileNo)) { JDEUtility jde = new JDEUtility(); userRole = Workforce.ResignationGetUserRole(employeeFileNo); Session[SessionKeys.UserCode] = employeeFileNo; if (!string.IsNullOrEmpty(userRole)) { Session[SessionKeys.RoleCode] = userRole; } if (userRole == RoleKey.LM || userRole == RoleKey.DM || userRole == RoleKey.LDM) { return RedirectToAction("PendingManagersRequests", "Resignation", null); } else { // Handle the case when userRole is not LM, DM, or LDM return View("Error"); } } else { ViewBag.errorMsg = "incorrect file no"; } } else { ViewBag.errorMsg = "unauthorized user"; } return View(); }With this fix, if the
userRoleis not LM, DM, or LDM, the action will return an "Error" view, allowing you to handle this case appropriately.Using Hidden Fields Instead of Session
If you want to pass the user role and user code from the
IndexResignationaction to thePendingManagersRequestsaction without using session, you can use hidden fields in the view.Here's an example of how you can modify the view and action to use hidden fields:
IndexResignation.cshtml:
@model YourModel @using (Html.BeginForm("PendingManagersRequests", "Resignation", FormMethod.Post)) { @Html.HiddenFor(model => model.UserRole) @Html.HiddenFor(model => model.UserCode) }In the above code, we're using the
Html.HiddenForhelper method to generate hidden fields for theUserRoleandUserCodeproperties of the model.ResignationController.cs:
[HttpPost] public async Task PendingManagersRequests(YourModel model) { // Access the user role and user code from the model string userRole = model.UserRole; int userCode = model.UserCode; // Rest of the action code return View(); }In the
PendingManagersRequestsaction, we're accepting theYourModelas a parameter and accessing the user role and user code from the model.