简体   繁体   English

Convert.ToDouble 异常:输入字符串的格式不正确

[英]Convert.ToDouble exception: Input string was not in a correct format

When i try to convert a number like 1.1 to a double i get thrown a exception: "Input string was not in a correct format."当我尝试将 1.1 之类的数字转换为双精度时,我得到了一个异常:“输入字符串的格式不正确。” I don't know why this is happening since the input i give is correct and the variable where i am converting to is a double.我不知道为什么会发生这种情况,因为我给出的输入是正确的,而我要转换的变量是双精度的。 Below is the code:下面是代码:

Controller create method: Controller 创建方法:

        [ValidateAntiForgeryToken]
        public ActionResult Create(IFormCollection collection)
        {
            try
            {
                Riskanalysis riskanalysis = new Riskanalysis()
                {
                    dateCreation = Convert.ToDateTime(collection["dateCreation"]),
                    userId = collection["userId"],
                    categoryId = Convert.ToInt32(collection["categoryId"]),
                    zoneId = Convert.ToInt32(collection["zoneId"]),
                    equipmentId = Convert.ToInt32(collection["equipmentId"]),
                    taskId = Convert.ToInt32(collection["taskId"]),
                    activity = collection["activity"],
                    action = collection["action"],
                    descriptionPotentialRisk = collection["descriptionPotentialRisk"],
                    dangerId = Convert.ToInt32(collection["dangerId"]),
                    consequenceId = Convert.ToInt32(collection["consequenceId"]),
                    dangerousEnergiesId = Convert.ToInt32(collection["dangerousEnergiesId"]),
                    preventionMeasures = collection["preventionMeasures"],
                    safetyProcedures = collection["safetyProcedures"],
                    preSeriousness = Convert.ToDouble(collection["preSeriousness"]),
                    preProbability = Convert.ToDouble(collection["preProbability"]),
                    preExposure = Convert.ToDouble(collection["preExposure"]),
                    preRiskDegree = Convert.ToDouble(collection["preRiskDegree"]),
                    preRiskCategory = Convert.ToDouble(collection["preRiskCategory"]),
                };

                DanoneRiskanalysisManager rm = new DanoneRiskanalysisManager();
                rm.InsertRiskanalysis(riskanalysis);

                return RedirectToAction(nameof(Create));
            }
            catch (Exception e)
            {
                return View();
            }
        } 

Input fields:输入字段:

                <div class="form-group">
                    <label asp-for="preSeriousness" class="control-label">Pre seriousness:</label>
                    <input asp-for="preSeriousness" class="form-control" type="text" required/>
                    <span asp-validation-for="preSeriousness" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="preProbability" class="control-label">Pre probability:</label>
                    <input asp-for="preProbability" class="form-control" type="text" required/>
                    <span asp-validation-for="preProbability" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="preExposure" class="control-label">Pre exposure:</label>
                    <input asp-for="preExposure" class="form-control" type="text" required/>
                    <span asp-validation-for="preExposure" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="preRiskDegree" class="control-label">Pre risk degree:</label>
                    <input asp-for="preRiskDegree" class="form-control" type="text" required/>
                    <span asp-validation-for="preRiskDegree" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="preRiskCategory" class="control-label">Pre risk category:</label>
                    <input asp-for="preRiskCategory" class="form-control" type="text" required/>
                    <span asp-validation-for="preRiskCategory" class="text-danger"></span>
                </div>

Anyone knows what the cause could be or where to look for?任何人都知道可能是什么原因或在哪里寻找?

Thanks already!已经谢谢了!

have you checked locale yet?, probably the number format doesn't use "."你检查过语言环境了吗?,可能数字格式不使用“。” As a decimal separator, try typing "," instead of "."作为小数分隔符,请尝试输入“,”而不是“。”

for more information see: support.microsoft有关详细信息,请参阅: support.microsoft

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM