+1 vote
ago by
there is a form
<form>
<div class="d-flex flex-row justify-content-between">
<div class="form-group">
<label asp-for="Price" class="control-label">Цена c Aliexpress</label>
<input asp-for="Price" class="form-control" type="number" id="Price" onchange="GetPrice()" />
<span asp-validation-for="Price" class="text-danger"></span>
</div>

<div class="form-group pl-3">
<label asp-for="SalePrice" class="control-label">Цена продажи</label>
<input asp-for="SalePrice" class="form-control" type="number" id="SalePrice" />
<span asp-validation-for="SalePrice" class="text-danger"></span>
</div>
</div>
</form>
and the function code
function GetPrice() {
const a = 0.8
const b = 0.7
var checkb = document.getElementById("chbox")
console.log(typeof (checkb))
if (checkb.checked)
{
let price = document.getElementById("Price").value * b
document.getElementById("SalePrice").value = Number(price.toFixed(2))
}
else
{
let price = document.getElementById("Price").value * a
document.getElementById("SalePrice").value = Number(price.toFixed(2))

}
}
when the values of the digits are integers or both fields are set manually, then all the data comes in. And when the values are floating point, the SalePrice data does not come.
ago by
webdisigner If it worked, I wouldn't be writing here.)
ago by
what is this genius solution
if (checkb.checked)
What is it, where is it in the code is not visible, if you ask something, then give the full code
ago by
webdisigner , It's a checkbox, and the rest of the code will only get in the way. This particular block does not work
ago by
eRKa , the solution was as follows, EF needed to prescribe a provider so that it could populate floating point numbers adequately.
ago by
I suspect that it may be a localization issue, how the delimiter comes, comma or dot. You would lay out exactly what error it gives or more accurately describe the steps and the result. It is necessary to debug, put breakpoints and watch what exactly is coming, and then think how to solve it. And the explanation "on the fingers" picture will not add up, no one can tell you the exact answer.

2 Answers

0 votes
ago by
 
Best answer
The solution was as follows, EF needed to prescribe a provider so that it could populate floating point numbers adequately.
//в стартапе
services.AddControllersWithViews(
options => options.ModelBinderProviders.Insert(0, new InvariantDecimalModelBinderProvider()));

public class InvariantDecimalModelBinderProvider : IModelBinderProvider
{
private readonly ILoggerFactory loggerFactory;

public IModelBinder GetBinder(ModelBinderProviderContext context)
{
if (context == null) throw new ArgumentNullException(nameof(context));

if (!context.Metadata.IsComplexType && (context.Metadata.ModelType == typeof(decimal) || context.Metadata.ModelType == typeof(decimal?)))
{
ILoggerFactory loggerFactory = context.Services.GetRequiredService<ILoggerFactory>();
return new InvariantDecimalModelBinder(context.Metadata.ModelType, loggerFactory);
}

return null;
}
}

public class InvariantDecimalModelBinder : IModelBinder
{
private readonly SimpleTypeModelBinder _baseBinder;

public InvariantDecimalModelBinder(Type modelType, ILoggerFactory loggerFactory)
{
_baseBinder = new SimpleTypeModelBinder(modelType, loggerFactory);
}

public Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext == null) throw new ArgumentNullException(nameof(bindingContext));

var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

if (valueProviderResult != ValueProviderResult.None)
{
bindingContext.ModelState.SetModelValue(bindingContext.ModelName, valueProviderResult);

var valueAsString = valueProviderResult.FirstValue;
decimal result;


if (decimal.TryParse(valueAsString, NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out result))
{
bindingContext.Result = ModelBindingResult.Success(result);
return Task.CompletedTask;
}
}

it
return _baseBinder.BindModelAsync(bindingContext);
}
}
0 votes
ago by
So do it. debugging .
Insert console.log() or at least alert() in the right places, and see where the intermediate values are.
ago by
Roman , specify in the question that it is about Entity Framework
ago by
Huh, if only it were that easy. It outputs values to the console. The problem here, as I understand it, is that EF doesn't populate SalePrice values (it's a decimal, it doesn't work with double either)
...