Answer the question
In order to leave comments, you need to log in
View value not coming?
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>
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))
}
}
Answer the question
In order to leave comments, you need to log in
The solution was the following, EF needed to register a provider so that it could map 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);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question