Sending via Formcollection of monetary values

Asked

Viewed 887 times

3

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

Figura 2
By clicking the green button, send via post, the values of the list below that generated the table:

@foreach (var item in Model.vmListPagamentos)
{
     <tr class="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 lies in the concatenation of monetary values. Formcollection, when passing the information to the controller, separates the values from the attribute "item.Paymentvalue" with commas. And the value of the field also possessed comma. To make it clearer look at the image below:

Método

I can’t imagine taking these two string values without using Split(','). But if I use the split to break this string in two, it will break into 4. Because I work with monetary value, I don’t want to keep going around concatenating these values. Is there another way to send values to a controller action other than via Formcollection? Why I also tried to send the Model as a parameter and it was not..

1 answer

4

For this you will need to add the package Begincollectionitem in the Nuget.

Install-Package Begincollectionitem

After that change your view for:

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

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

By doing this, you need to create the Partialview _Payment.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 his controller you send your Model, being like this:

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

Once this is done, the entered values will all be within model.vmListPagamentos. Just make a foreach and use as intended.

In this research you find several answers, here at Sopt, about the use.

  • Thanks for the feedback, Randrade. I tried to use the schematic you gave me, but I’m running into a mistake. "=> item" is in error in Partialview, displaying the following message: "The name 'item' does not exist in the Current context" It was referenced on the page as you directed. @foreach (var item in Model.vmListPayments) { @Html.Partial("_Payment", item) }

  • @Gleisoconfidence Sorry, my fault. Change the item for Model. I changed the answer so you’d have an idea.

  • Thanks to the return Randrade, I was able to display the screen with the filled out list without errors following his scheme. When clicking the button the event is triggered and access the action. However, the Model parameter is null. What did I do wrong? I have a list inside a viewModel that follows this pattern: public List<VW_PAGAMENTOS> vmListPagamentos { get; set; }

Browser other questions tagged

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