问题
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." 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:
[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!
回答1:
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
来源:https://stackoverflow.com/questions/61486977/convert-todouble-exception-input-string-was-not-in-a-correct-format