Search code examples
c#razorasp.net-core-mvc

EditorFor not allowing to enter decimal value


I have model that has decimal field

[DisplayFormat(DataFormatString = "{0:0.00}", ApplyFormatInEditMode = true)]
public decimal Sum { get; set; }

And I want to have input field created in which user can put decimal value with two decimal places

[HttpGet]
public IActionResult TransferForm()
{
    string serialized = HttpContext.Session.GetString("CurrentUser") as string;
    UserData user = JsonConvert.DeserializeObject<UserData>(serialized);
    Transfer newTransfer = new Transfer();
    newTransfer.Date = DateTime.Now;
    if (user.Balance + 2000 < 0)
    {
        TempData["MaxValue"] = 0;
    }
    else
    {
        TempData["MaxValue"] = user.Balance + 2000;
    }
    newTransfer.Sender = user.FirstName + " " + user.Surname;
    ViewData.Model = newTransfer;
    return View();
}
<div class="form-group">
    @Html.LabelFor(model => model.Sum, "Kwota", htmlAttributes: new { @class = "col-sm-2 control-label" })
    <div class="col-sm-10">
        @Html.EditorFor(model => model.Sum, new { htmlAttributes = new { @class = "form-control", @placeholder = "Kwota", @type = "number", @min = "0.00", @max = TempData["MaxValue"], @step=0.01 } })
    </div>
</div>

The problem is that without step attribute, I cannot enter decimal value, only int are correct. I somehow forced the field to accept decimal values with step attribute but with this attribute, when user press submit button, null is readed.

GET endpoint for this form I read object of UserData from session to

Here is the page:

@model Solution.Models.Transfer
@using (Html.BeginForm("TransferForm", "Home", FormMethod.Post))
{
<div class="form-horizontal">
    <h2>Zrób przelew</h2>
    <!--Pole na tytuł-->
    <div class="form-group">
        @Html.LabelFor(model => model.Title, "Tytuł", htmlAttributes: new { @class = "col-sm-2 control-label" })
        <div class="col-sm-10">
            @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control", @placeholder = "Tytuł" } })
        </div>
    </div>
<div class="form-group">
    @Html.LabelFor(model => model.Sum, "Kwota", htmlAttributes: new { @class = "col-sm-2 control-label" })
    <div class="col-sm-10">
        @Html.EditorFor(model => model.Sum, new { htmlAttributes = new { @class = "form-control", @placeholder = "Kwota", @type = "number", @min = "0.00", @max = TempData["MaxValue"], @step=0.01 } })
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(model => model.Date, "Data", htmlAttributes: new { @class = "col-sm-2 control-label" })
    <div class="col-sm-10">
        @Html.EditorFor(model => model.Date, new { htmlAttributes = new { @class = "form-control", @disabled = "disabled" } })
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(model => model.Sender, "Nadawca", htmlAttributes: new { @class = "col-sm-2 control-label" })
    <div class="col-sm-10">
        @Html.EditorFor(model => model.Sender, new { htmlAttributes = new { @class = "form-control", @disabled = "disabled" } })
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(model => model.Recipient, "Odbiorca", htmlAttributes: new { @class = "col-sm-2 control-label" })
    <div class="col-sm-10">
        @Html.EditorFor(model => model.Recipient, new { htmlAttributes = new { @class = "form-control", @placeholder = "Odbiorca" } })
    </div>
</div>

<div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
        <input type="submit" value="Potwierdź" class="btn btn-dark" />
    </div>
</div>

Transfer.cs

public class Transfer
{
    public string Title { get; set; }

    [DisplayFormat(DataFormatString = "{0:0.00}", ApplyFormatInEditMode = true)]
    public decimal Sum { get; set; }
    public DateTime Date { get; set; }
    public string Recipient { get; set; }
    public string Sender { get; set; }

    public Transfer()
    {

    }

    public Transfer(string title, decimal sum, DateTime date, string recipient, string sender)
    {
        Title = title;
        Sum = sum;
        Date = date;
        Recipient = recipient;
        Sender = sender;
    }

}

Solution

  • You are declared your view data model by the @model declaration (strongly typed view). Therefore, instead of ViewData.Model = newTransfer; pass the data model to the TransferForm view as following:

    public IActionResult TransferForm()
    {          
        ...
        return View(newTransfer);
    }
    

    Fragment of the TransferForm.cshtml:

    ...
    @using (Html.BeginForm("TransferForm", "Home", FormMethod.Post))
    {
        @* Two lines below added to support the data binding for the Post... *@
        @Html.HiddenFor(m => Model.Sender)
        @Html.HiddenFor(m => Model.Date)
        ...
    

    And the screenshot:

    enter image description here