A
A
Alyosha2018-02-04 11:02:19
SQL
Alyosha, 2018-02-04 11:02:19

How to insert values ​​with dot or comma in ASP.NET Core 2.0?

I think that my question is the top one for today.
https://github.com/dignatukhin/albeor1.git
Microsoft is a top company you say? well, yes, they also have top encoders (ASP.NET Core 2.0). Watafak, how to fix it?
In the model:

[Required]
        public decimal Price { get; set; }

In controller:
[HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("ID,Title,ReleaseDate,Genre,Price, Rating")] Movie movie)
        {
            if (ModelState.IsValid)
            {
                _context.Add(movie);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(movie);
        }

In HTML:
<div class="form-group">
                <label asp-for="Price" class="control-label"></label>
                <input asp-for="Price" class="form-control" />
                <span asp-validation-for="Price" class="text-danger"></span>
            </div>

07tMJXmbqho.jpg

Answer the question

In order to leave comments, you need to log in

5 answer(s)
M
mletov, 2018-02-26
@digna

1) Try float or double
2) For input add type="text"
UPD
This is how it works if you enter it separated by commas

[DataType(DataType.Currency, ErrorMessage = "Must be a Deciaml!")]
public float Price { get; set; }

A
Alexander, 2018-02-04
@alexr64

The fact that you marked a field as [Required] only means that this field must be filled in. However, you did not specify the check logic. It's Microsoft's fault...

D
Dmitry Bashinsky, 2018-02-05
@BashkaMen

Perhaps a double?

A
Alyosha, 2018-02-04
@digna

AleXr64 on GitHub suggested adding [RegularExpression(@"^\d+.?\d{0,2}$", ErrorMessage = "Invalid Target Price; Maximum Two Decimal Points.")]
Maybe someone is interested, but this one option didn't work either.

B
bla_bla_bla, 2019-08-28
@bla_bla_bla

If you need to bind a floating point value, then this is how you can:
https://github.com/badhitman/AspDotNetCore2BinderF...
It will not work for client-side validation for JS, but the server will work correctly.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question