简体   繁体   中英

can't pass decimal value to model parameter

I have a Dto Like this

public class AgeDto
{
    public long Id { get; set; }
    [Required]
   
    [Precision(2, 1)]
    public decimal LowAge { get; set; }

    [Precision(2, 1)]
    public decimal? HighAge { get; set; }
}

and my for is like this

 @model AgeDto
 <form class="form-account" method="post" asp-action="Index" asp-controller="ages">
                        <div class="d-inline-block w-25">
                            <div class="label-form "> From</div>
                            <div>
                                <input asp-for="LowAge" type="text" class=" w-50">
                                <span asp-validation-for="LowAge" class="text-danger star-red"></span>
                            </div>
                        </div>

                        <div class="d-inline-block w-25">
                            <div class="label-form "> To</div>
                            <div>
                                <input asp-for="HighAge" type="text" class=" w-50">
                                <span asp-validation-for="HighAge" class="text-danger star-red"></span>
                            </div>
                        </div>
                        <div  class="d-inline-block w-25">

                            <div class="d-inline-block px-3">
                                <input asp-for="Id" type="hidden" />
                                <input class="btn btn-primary btn-register" type="submit" value="Register" />
                            </div>

                        </div>
                    </form>

and controller like this

   [HttpPost]
    public async Task<IActionResult> Index(AgeDto agesDto)
    {
    //           does something
    }

althought the LowAge and HighAge are decimal, but if enter 1.5 or 2.5 or any number with fractional it passes value 0 on agesDto on Controller why is that so?

0 is the default value for your model properties. It's most likely the modelbinding doesnt recognize any valid values to apply to the properties.

Most of the time this is due to culture conflicts for the separator, especially since you're using text-inputs for the properties.

See: https://stackoverflow.com/a/53120061/6803592

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM