Is it possible to send data from the typed view with IEnumerable to the Controller? [duplicate]

2

I'm sending data using a get and get in the controller using a QueryString, the problem is that the value goes in a format without the decimal places, so when I have a value of type 10,20, it is sending only 1020 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>
}
    
asked by anonymous 09.01.2016 / 22:22

2 answers

1

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

Sent the value 10,20

It was being done like this:

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

Answer: 1020

But if I get the result in a string, I'll have the result in the same form as typed:

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

Answer: 10.20

At the time of writing to the bank I need to do the processing as follows:

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

This solves the problem.

    
10.01.2016 / 00:10
1

Install the DecimalModelBinder package.

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 sent value will be DecimalModelBinder . The class already checks which decimal separator is used by the culture configured in the project and does it all by itself.

    
09.01.2016 / 22:26