ahmed salah

ahmed salah

  • 1.2k
  • 547
  • 64.2k

error Input string not in correct format when click submit button

Jan 24 2024 1:09 AM

I work on asp.net mvc application . i get error input string not in correct format

i don't know what is the reason of this issue and which line give me this issue

on local pc issue not happen but on iis publish site web app it happen

so how i know reason of this issue please

and can i do some enhancement on code to display more clear message or prevent this error from happen

on which line

my action code as below

public JsonResult RequesterIndex(ResignationRequester resignationRequester)
{
dynamic responseData = new ExpandoObject();
responseData.success = false;
responseData.message = "";
try
{
var filenumber = resignationRequester.EmpID;
if (Session[SessionKeys.UserCode] != null)
{
JDEUtility jde = new JDEUtility();
resignationRequester.EmpID = Convert.ToInt32(Session[SessionKeys.UserCode]);
resignationRequester.Gender = Convert.ToString(Session[SessionKeys.Gender]);
if (ModelState.IsValid)
{
if (Convert.ToString(resignationRequester.LineManager).Length < 6 && !string.IsNullOrEmpty(resignationRequester.LineManager.ToString()))
{
responseData.success = false;
responseData.message = "Length Line Manager Must Be equal 6 or More";
return Json(responseData);
}
if (Convert.ToString(resignationRequester.DirectManager).Length < 6 && !string.IsNullOrEmpty(resignationRequester.DirectManager.ToString()))
{
responseData.success = false;
responseData.message = "Length Direct Manager Must Be equal 6 or More";
return Json(responseData);
}
if (Convert.ToInt32(resignationRequester.EmpID) == Convert.ToInt32(resignationRequester.DirectManager) &&
Convert.ToInt32(resignationRequester.EmpID) == Convert.ToInt32(resignationRequester.LineManager)
&& !string.IsNullOrEmpty(Convert.ToString(resignationRequester.LineManager)) && !string.IsNullOrEmpty(Convert.ToString(resignationRequester.DirectManager)))
{
responseData.success = false;
responseData.message = "Requester Have Same Number of Manager";
return Json(responseData);
}
if (!string.IsNullOrEmpty(Convert.ToString(resignationRequester.LineManager)))
{
responseData.success = false;
responseData.message = "Line Manager Name Blank";
return Json(responseData);
}
if (string.IsNullOrEmpty(ViewBag.errorMsg))
{
responseData.success = true;
responseData.message = "Resignation Submission form Created successfully";
TempData["SuccessBeforePrint"] = 1;
return Json(responseData);
}
}
else
{
responseData.success = false;
var errors = ModelState.Select(x => x.Value.Errors)
.Where(y => y.Count > 0)
.ToList();
responseData.message = "Some Required Fields Not Added";
return Json(responseData);
}
}
}
catch (System.FormatException ex)
{
responseData.success = false;
responseData.message = ex.Message;
}
return Json(responseData);
}

on ui this is what i do

$("#txtLineManagerId").autocomplete({
source: function (request, response) {
var searchText = $("#txtLineManagerId").val();
console.log("search text" + searchText)
$.ajax({
url: '@Url.Action("GetAllEmployeeBasedSearchText", "Resignation")',
data: { searchText: searchText },
method: "GET",
dataType: "json",
success: function (data) {
if (!data.length) {
var result = [
{
label: 'No matches found',
value: response.term
}
];
response(result);
}
else {
response($.map(data, function (item) {
return {
label: "File No : " + item.EmployeeID + " - " + "Name :" + item.EmployeeName + " - " +
"Designation : " + item.Designation, value: item.EmployeeID,
employeeName: item.EmployeeName,
designation: item.Designation
};
}))
}
}
});
},
position: { my: "right top", at: "right bottom" },
appendTo: '#searchContainer',
select: function (event, ui) {
$("#LineManagerName").val(ui.item.employeeName);
},
minLength: 2,
}).data("ui-autocomplete")._renderItem = function (ul, item) {
console.log("after autocomplete" + item);
return $("<li>")
.append("<div>File No: " + item.value + "<br/>Name: " + item.employeeName + "<br/>Designation: " + item.designation + "</div>")
.appendTo(ul);
};
<div class="form-group col-md-6">
<div class="col-md-5">
@Html.LabelFor(model => model.LineManager, htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-7">
@Html.EditorFor(model => model.LineManager, new
{
htmlAttributes = new
{
@class = "form-control"
,
id = "txtLineManagerId"
}
})
<div id="searchContainer">
</div>
</div>
</div>
public class ResignationRequester
{
[Display(Name = "Employee No : ")] [Required,]
public int EmpID { get; set; }
[Display(Name = "Line Manager: ")]
public int? LineManager { get; set; }
}

Answers (1)