Decimals in mvc


How about. I am developing a web application in Mvc, and it is a medical calculator, where the user enters certain values, the account is made, and a value is returned. In the cases where the entry has to be with decimals, I get the following error:

El diccionario de parámetros contiene una entrada NULL para el parámetro 'globroj' del tipo que no acepta valores NULL 'System.Decimal' del método 'System.Web.Mvc.ActionResult CalcularHematocritos(System.Decimal, System.Decimal)' en 'Griersonmvc.Controllers.HomeController'. Un parámetro opcional debe ser un tipo de referencia, un tipo que acepte valores NULL o debe declararse como parámetro opcional.Nombre del parámetro: parameters

With the rest of the numbers, it's perfect. probe putting the point in the number instead of the comma, but neither. I leave the form and the driver of this case ..


    public ActionResult CalcularHematocritos(decimal globroj, decimal volsan)
     decimal resultadoh = (globroj / volsan)*100;

        ViewBag.Resultadoh = resultadoh;
        return View("resultadoh");


And the HTML is this

 <form action="@Url.Action("CalcularHematocritos" , "Home")" method="post">

                                    <label for="number">
                                        <span class="required">Volumen de Globulos Rojos *</span>
                                        <input type="number" step="0.01"  id="globroj" name="globroj" value="" placeholder="Vol. Glob. Roj." required="required" tabindex="1" autofocus="autofocus" />
                                    <label for="number">
                                        <span class="required">Volumen Sangre</span>
                                        <input type="number" step="0.01"  id="volsan" name="volsan" value="" placeholder="Vol. San." tabindex="2" required="required" />

                                        <button name="submit" class="btn btn-success" type="submit" id="submit">Calcular </button>



asked by Denu Lemos 22.12.2017 в 02:12

1 answer


The problem is that you are executing a request of type POST and the variables are received as parameters in the Controller, if it were a request GET you would not have any problem. The point here is that the data sent from the form travel in the body of the request and you only have to add the [FromBody] attribute so that you can receive them as you expect.

Controller Code:

using System.Web.Http;
public ActionResult CalcularHematocritos([FromBody]decimal globroj, [FromBody]decimal volsan)
    ViewBag.Resultado = (globroj / volsan) * 100;
    return View();

View Code:

    ViewBag.Title = "Calcular Hematocritos";

<h2>El resultado del cálculo es: @ViewBag.Resultado</h2>

Note: if you mark an error when adding the attribute [FromBody] , you have to install the library from the NutGet Package Console:

  • Menu Tools --> NutGet Package Manager --> NutGet Package Console
  • Paste Install-Package Microsoft.AspNet.WebApi.Core -Version 5.2.3 and give the key Enter
  • answered by 22.12.2017 в 06:12