Is it possible to send Ienumerable typed view data to the Controller?

Asked

Viewed 286 times

2

I am sending data using a get and receive in the controller using a Querystring, the problem is that the value goes in a format without the decimals, so when I have a value like 10.20, this sending only 1020 recording wrong value.

@model IEnumerable<Generico.Dominio.TB_JOGO_DETALHE_TEMP>

@{
    ViewBag.Title = "Index";
}

@Html.Partial("_navbarInterno")
@Html.Partial("_PartialMensagens")




@using (Html.BeginForm("GravarDados", "Aposta", FormMethod.Get))
{
    <div class="container droppedHover">
        <div class="row">
            <div class="input-prepend input-append">
                <input class="form-control input-sm " onkeyup="somenteNumeros(this);" id="numero" name="numero"  placeholder="número.." maxlength="4" type="text"/>
                <input class="form-control input-sm" type="number"  id="valor" name="valor" placeholder="valor.."/>
            </div>
        </div>
        <br />
        <button class="btn btn-primary btn-block " type="submit" >Adicionar</button>
    </div>
}

2 answers

1

Install the package Decimalmodelbinder.

In Global.asax.cs, method Application_Start, add the following:

ModelBinders.Binders.Add(typeof(decimal), new DecimalModelBinder());
ModelBinders.Binders.Add(typeof(decimal?), new DecimalModelBinder());

When sending any type of decimal data to your Controller, the class used to handle the value sent will be the DecimalModelBinder. The class already checks which decimal separator is used by the culture configured in the project and does it all by itself.

  • Could you tell me how this can solve the problem? , I just want to send the data the way it is in the input

  • I updated the answer.

1


When you send the views data to the controller via GET method as it is in the question, at the time of receiving the result, I should receive exactly how it comes is not to make a value conversion:

Sent the value 10.20

was being made like this:

decimal valor = Convert.ToDecimal(Request.QueryString["valor"]);

answer : 1020

but if I receive the result in a string, I will have the result in the same formed typed:

string valor = Request.QueryString["valor"];

answer : 10.20

At the time of recording in the bank I need to do the treatment as follows:

string.Format(CultureInfo.InvariantCulture, " {0:0.00}, ", VALOR_JOGO);

that solves the problem .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.