R
R
Roman2021-03-26 01:28:37
JavaScript
Roman, 2021-03-26 01:28:37

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>

and 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 numbers are integers or both fields are set manually, then all data is received.
And when the values ​​are floating point, then the SalePrice data does not come.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman, 2021-03-27
@prorom-exe

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);
        }
    }

D
dollar, 2021-03-26
@dollar

So fix it .
Insert console.log() or at least alert() in the right places, and see where are the intermediate values.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question