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 .
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
Jayraj ChhayaPosted Jan 10, 2024, 9:09 AM
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: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:
In the above code, we're using the
Html.HiddenForhelper method to generate hidden fields for theUserRoleandUserCodeproperties of the model.ResignationController.cs:
In the
PendingManagersRequestsaction, we're accepting theYourModelas a parameter and accessing the user role and user code from the model.Naimish MakwanaPosted Jan 10, 2024, 4:42 AM
Sure, you can use hidden fields to pass data from one action to another. Here’s how you can modify your
IndexResignationaction andPendingManagersRequestsaction: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:In this form,
userCodeandroleCodeare stored in hidden fields. When the form is posted, these values will be sent to the server12.Thanks