ahmed salah

ahmed salah

  • 1.2k
  • 547
  • 65.2k

Why cast employee id to session user code failed and give invalid fil

Jan 29 2024 8:18 PM

I work on asp.net mvc web application . i face issue on RequesterIndex action with type Post when call it by ajax request it give me

message Invalid File No although I assigned value of session user code to Employee Id when click submit button action Requester

index with type post fire then it give me message Invalid File No why this message display for me . I assign value of session user code

to employee id also i store employee id on hidden field to avoid reset when submit my link use to access requester index action on resignation controller

so Why message Invalid File No display when click submit button this is my question . my code for action and view related to issue as below :

public class ResignationController : Controller
{
public class ResignationRequester 

{     

[Display(Name = "Employee No : ")]     [Required,]     

public int EmpID { get; set; }

}
//get method
public ActionResult RequesterIndex(string filenumber)
{
   
    int employeeFileNo;

    if (!string.IsNullOrEmpty(filenumber))
    {
      
        if (int.TryParse(filenumber, out employeeFileNo))
        {
       
            Session[SessionKeys.UserCode] =  employeeFileNo;
           
             

                resignationRequester.EmpID = Convert.ToInt32(Session[SessionKeys.UserCode].ToString());
                Session[SessionKeys.UserCode] =  employeeFileNo;
        }
               
        
    }
    else
    {
        ViewBag.errorMsg = "unauthorized user";
    }
    return View(resignationRequester);
}

//post method
public JsonResult RequesterIndex(ResignationRequester resignationRequester)
{
    dynamic responseData = new ExpandoObject();
    responseData.success = false;
    responseData.message = "";
    try
    {
        var filenumber = resignationRequester.EmpID;
        if (Session[SessionKeys.UserCode] != null)
        {
            
            if (int.TryParse(Session[SessionKeys.UserCode].ToString(), out int empId))
            {
              
                resignationRequester.EmpID = empId;
            }
            else
            {
                
                responseData.success = false;
                responseData.message = "Invalid File No";
                return Json(responseData);
            }
            
           
        }
        else
        {
            responseData.success = false;
            responseData.message = "No Data For This File No";
            return Json(responseData);
        }
    }
   
    catch (System.FormatException ex)
    {
        responseData.success = false;
        responseData.message = ex.Message;
    }
    return Json(responseData);
    
}
}

requesterIndex.cshtml

<h2 style="font-weight: bold; padding-bottom: 5px;padding-top:50px;">Resignation Form</h2>
<form id="ResignationApp" style="padding-top: 50px; border: 2px solid black;padding-left:20px;font-size:16px;">
    @Html.AntiForgeryToken()

        <div class="row">
           
            <div class="form-group col-md-6">
                <div class="col-md-5">
                    @Html.LabelFor(model => model.EmpID, htmlAttributes: new { @class = "control-label" })
                </div>

                <div class="col-md-7">
                    @Html.EditorFor(model => model.EmpID, new { htmlAttributes = new { @class = "form-control", id = "txtempid", @readonly = "readonly" } })
                  
                  
                    @Html.HiddenFor(model => model.EmpID, new { htmlAttributes = new { @class = "form-control", id = "hiddentxtempid", @readonly = "readonly" } })
                </div>
            </div>
          

        </div>
 <div class="col-md-7">
     <input id="btnsubmit" value="Submit" type="submit" style="background-color: #05014a;font-size:18px;margin-top:20px; color: #fff; height: 40px; width: 200px;margin-bottom:40px;" />
 </div>
</form>

<script>
 $(document).ready(function () {
     var value = $("#txtempid").val();
     $("#hiddentxtempid").val(value);
        $('#btnsubmit').click(function () {
     
            var empidval = $("#txtempid").val();
            var formData = $("#ResignationApp").serialize();
                $.ajax({
                    type: "POST",
                    dataType: 'json',
                    url: '@Url.Action("RequesterIndex", "Resignation")',
                    data: formData,
                    success: function (response) {  
                    },
                    error: function (xhr, status, error) {
                    }
                   
                });
                
        });
        $("#ResignationApp").submit(function (e) {
            e.preventDefault();
        });
});
</script>

 


Answers (3)