当dropdownlist设置为default时,如何修复System.FormatException? [英] How to fix a System.FormatException when dropdownlist is set to default?

查看:82
本文介绍了当dropdownlist设置为default时,如何修复System.FormatException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了"mscorlib.dll中发生类型'System.FormatException'的异常,但未在用户代码中处理."附加信息:输入字符串的格式不正确.我有一个下拉列表#Carriers,当用户选择一个项目时不会发生任何错误,但是,当用户选择下拉列表为默认值时,它将显示此错误.我需要一种写if语句来检查空值的方法.

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; 
    }

推荐答案

在调用Convert.ToInt32方法之前,您需要检查string参数的值并确保它是可以安全转换为int的某个值.值.

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方法会很方便

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);
}

carrierId参数值不是有效的数字字符串值时,以上代码当前返回SelectListItem的空列表.更新代码以根据需要返回所有内容(不进行过滤).

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.

我还建议使用适当的类型.如果carrierId始终是int值或没有值,则可以考虑使用可为null的int,并避免对字符串调用TryParse方法.

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
}

这篇关于当dropdownlist设置为default时,如何修复System.FormatException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆