Send via FormCollection of monetary values

3

I need to send to a method in the Controller the values filled in the Payment_Value column as shown below:

HomeWhenclickingthegreenbutton,sendviapost,thevaluesofthelistbelowthatgeneratedthetable:

@foreach(variteminModel.vmListPagamentos){<trclass="odd gradeX">
           <td>@Html.DisplayFor(modelItem => item.Matricula)</td>
           <td>@Html.DisplayFor(modelItem => item.Nome)</td>
           <td>@Html.DisplayFor(modelItem => item.Empresa_DtEntrada)</td>
           <td>@Html.DisplayFor(modelItem => item.Empresa_DtSaida)</td>
           <td>@Html.TextBoxFor(modelItem => item.Pagamento_Valor)</td>
           <td>@Html.TextBoxFor(modelItem => item.DtPagamento)</td>
     </tr>
}

The problem is in the concatenation of monetary values. The FormCollection, when passing the information to the controller, separates the values of the attribute "item.PaymentValue" with commas. And the value of the field also has a comma. To make it clearer, look at the image below:

I can not figure out how to get these two string values without using Split (','). But if I use split to break this string in two, it will break by 4. For working with monetary value, I do not want to be doing any gambiarra to stay concatenating those values. Is there another way to do this send values to an action in the controller without being via FormCollection? Why I also tried to send the Model as a parameter and it was not ..

    
asked by anonymous 24.09.2015 / 23:03

1 answer

4

For this, you will need to add the BeginCollectionItem package to Nuget .

  

Install-Package BeginCollectionItem

After this change your view to:

@using (Html.BeginForm("PagamentoConfirmed3", "Controller"}))
{

    @foreach (var item in Model.vmListPagamentos)
    {
        @Html.Partial("_Pagamento", item)
    }
}

By doing this, you need to create the PartialView _Page.cshtml

Your PartialView will look like this:

@model Models.Pagamento// Seu Model vmListPagamentos aqui

@using (Html.BeginCollectionItem("vmListPagamentos"))
{
   <tr class="odd gradeX">
       <td>@Html.DisplayFor(modelItem => Model.Matricula)</td>
       <td>@Html.DisplayFor(modelItem => Model.Nome)</td>
       <td>@Html.DisplayFor(modelItem => Model.Empresa_DtEntrada)</td>
       <td>@Html.DisplayFor(modelItem => Model.Empresa_DtSaida)</td>
       <td>@Html.TextBoxFor(modelItem => Model.Pagamento_Valor)</td>
       <td>@Html.TextBoxFor(modelItem => Model.DtPagamento)</td>
 </tr>
}

And in your controller you send your Model , thus:

[httpPost]
public ActionResult PagamentoConfirmed3(Model model){...}//Passe seu Model aqui

Once this is done, the typed values will all be within model.vmListPagamentos . Just make a foreach and use as you see fit.

In this search you find several answers, here in SOpt, about usage.

    
25.09.2015 / 00:28