i'm stuck and cannot find easy way to solve this issue, let's suppose i have below model:
public class FooModel
{
string name{get;set;}
double doubleFoo{get;set}
}
i built a class, usable from a HTMLform, where user can upload an excel file, and this class will read values to create a new fooObject, with properties like FooModel.
FooModel FooObjectFromExcelValues = new FooModel()
{
name = nameFromExcel,
doubleFoo = doubleFooFromExcel, /* <= this variable contains dot decimal separation, and look like "0.061065380952380954" */
}
I have checked, FooObjectFromExcelValues.doubleFooFromExcel is dot separated value.
Now, my controller return a view, where a form is populated with values sent from FooObjectFromExcelValues.
<form asp-action="FooAdd">
<div>
<label asp-for="name" class="control-label"></label>
<input asp-for="name" class="form-control"/>
<span asp-validation-for="name" class="text-danger"></span>
</div>
<div>
<label asp-for="doubleFoo " class="control-label"></label>
<input asp-for="doubleFoo " class="form-control"/>
<span asp-validation-for="doubleFoo " class="text-danger"></span>
</div>
</form>
form once populated with object sent from excel
here come the trouble, doubleFoo is now showed on my form with comma separation like "0,061065380952380954", and i can't send the form to add object to database.
Why ? How can i easily solve this ?
tried several from stackoverflow without success
doubleFoo is now showed on my form with comma separation like "0,061065380952380954", and i can't send the form to add object to database.
Why ? How can i easily solve this ?
Well, in some locales, commas are used as decimal separators instead of dots. This discrepancy causes issues when the form data is sent back to the server, as the server expects a dot-separated decimal value.
I order to fix the issue, in the controller, convert the double value to a string with a dot as the decimal separator before passing it to the view. This ensures that the correct format is displayed in the form.
You could do as following:
var fooObjectFromExcelValues = new FooModel
{
name = "Sample Name",
doubleFoo = 0.061065380952380954
};
ViewBag.DoubleFooString = fooObjectFromExcelValues.doubleFoo.ToString(CultureInfo.InvariantCulture);
View:
@model FooModel
<form asp-action="FooAdd" method="post">
<div>
<label asp-for="name" class="control-label"></label>
<input asp-for="name" class="form-control" />
<span asp-validation-for="name" class="text-danger"></span>
</div>
<div>
<label asp-for="doubleFoo" class="control-label"></label>
<input asp-for="doubleFoo" class="form-control" value="@ViewBag.DoubleFooString" />
<span asp-validation-for="doubleFoo" class="text-danger"></span>
</div>
<button type="submit">Submit</button>
</form>
When you would submit the from value, you could cross check the correct value once again:
if (double.TryParse(model.doubleFoo.ToString(CultureInfo.InvariantCulture), NumberStyles.Float, CultureInfo.InvariantCulture, out double parsedDouble))
{
model.doubleFoo = parsedDouble;
// Simulate adding the object to the database
// db.FooModels.Add(model);
// db.SaveChanges();
return RedirectToAction("Index");
}
Output:
Note: Please refer to this official document for additional information.