How to fix a System.FormatException when dropdownlist is set to default?

可紊 提交于 2020-01-22 03:41:10

问题


I am having "An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code." Additional information: Input string was not in a correct format. I have a dropdown list, #Carriers, when user selects an item no errors occur, however, when user selects the dropdownlist back to default it show this error. I need a way to write an if statement to check for null values.

 [AcceptVerbs(HttpVerbs.Get)]
    public JsonResult LoadAccsByCarrierId(string carrierid)
    {

            var accsList = this.GetAccs(Convert.ToInt32(carrierid));
            var accsData = accsList.Select(m => new SelectListItem()
            {
                Text = m.AccessoryName,
                Value = m.AccessoryID.ToString(),
            });
            return Json(accsData, JsonRequestBehavior.AllowGet);
    }

     [AcceptVerbs(HttpVerbs.Get)]
    public JsonResult LoadPhonesByCarrierId(string carrierid, string emailaddress)
    {
        int id;
        var phonesData = new List<SelectListItem>();
        if (Int32.TryParse(carrierid, out id))
        {
            var phonesList = this.GetPhones(id, emailaddress);
            phonesData = phonesList.Select(m => new SelectListItem()
            {
                Text = m.Name,
                Value = m.PhoneID.ToString(),
            }).ToList();
            return Json(phonesData, JsonRequestBehavior.AllowGet);
        }
        else
            return null; 
    }

回答1:


Before calling the Convert.ToInt32 method, you need to check the value of the string parameter and make sure it is some value which can be safely converted to an int value.

Int32.TryParse method will he handy

public JsonResult LoadAccsByCarrierId(string carrierid)
{
     int id;
     var accsData =new List<SelectListItem>();
     if (Int32.TryParse(carrierid, out id))
     {
        var accsList = this.GetAccs(id);
        accsData = accsList.Select(m => new SelectListItem()
        {
            Text = m.AccessoryName,
            Value = m.AccessoryID.ToString(),
        }).ToList();
     }
     return Json(accsData, JsonRequestBehavior.AllowGet);
}

The above code currently returns an empty list of SelectListItem when the carrierId parameter value is not a valid numerical string value. Update the code to return everything (no filtering) as needed.

I also suggest to use the appropriate types. If carrierId is going to be always an int value or no value, you might consider using a nullable int and avoid the TryParse method call on string.

public ActionResult LoadAccByCarrierId(int? carrierId)
{
  if(carrierId!=null)
  {
       // to do : use carriedId.Value to do the Filtering
  }
  else
  {
    return something else 
  }
  // to do  : Return something
}


来源:https://stackoverflow.com/questions/42836896/how-to-fix-a-system-formatexception-when-dropdownlist-is-set-to-default

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!