I cannot get the Selected Dropdownlistfor element inside a Partialview to send to the Controller via Actionlink

Asked

Viewed 31 times

0

Hello

I have a Partialview that is filled with selected entries through previous filters. partial view is no more than the presentation of a table with multiple rows in which each row will be a record and will have a Dropdownlistfor and an update button on each of these lines/record.

The goal is to be able to update the field by dropdownlistfor and then write to the comic with the update button.

Here is the code of the partial_view:

<table id="table" class="table">
        <tr>
            <th>Id</th>
            <th>Descrição</th>
            <th>Início</th>
            <th>Fim</th>
            <th>Origem</th>
            <th>Estado</th>
            <th></th>
            <th></th>
        </tr>
        @if (Model.listGestaoAfericoes != null)
        {
            foreach (var item in Model.listGestaoAfericoes)
            {

                <tr id="@item.id">
                    <td id="id">@item.id</td>
                    <td>@item.descricao</td>
                    <td>@item.data_ini_afericao</td>
                    <td>@item.data_fim_afericao</td>
                    <td id="origem">@item.origem_afericao</td>
                    <td>@item.id_estado_actual</td>

                    <td>Estado: @Html.DropDownListFor(model => model.SelectedEstadoRow, (IEnumerable<SelectListItem>)Model.listEstados, "Seleccione um Estado")</td>
                    <td>

                        @Html.ActionLink("Alterar Estado", "AlterarEstadoAfericao", new { item.id, @Model.SelectedEstadoRow },
    new { onclick = "return confirm('Tem a certeza que pretende alterar o estado?');", @class = "btn btn-info" })

                    </td>
                </tr>
            }
        }

    </table>

Here is the controller code:

 public ActionResult AlterarEstadoAfericao(int id, string SelectedEstadoRow)
 {

 }

What happens is that the selectedStatesRow value always passes null to the controller and does not collect the value (String state type) of the dropdownlistfor. If this dropdownlistis filled correctly by about 10 elements.

Someone knows how to help me get the specific registration selected by the dropdownlistfor of each Row and subter that registration at the time of clicking on Actionlink?

Regards

  • The DropDownListFor as well as the ActionLink are rendered on the server side before the user makes their selection. You’ll need to post to the controller or mount this link manually by capturing the information via JavaScript

  • Right however the question I refer to is which model.Selectedestadorow supposedly obtained by Dropdownlistfor is Null when inserted into Actionlink @Model.Selectedestadorow

  • How would it be easier to perform this action? Can you give me an example? Either via Post or via Javascript?

  • It is null because it is running before the control is rederized on the screen

  • I am already able to pass Viewmodel, however I need to fill in a field "idSelected" of Viewmodel with the value of @item.id, how can I do this in the view? I’m not getting it

  • Or better I can send the id, but when the table has more than 1 row the item.id takes the id of the first element of the row instead of the id of the row corresponding to the change...

  • That’s another completely different problem

  • And do you have any idea how to help me solve this problem?

  • Depends, what makes the method AlterarEstadoAfericao(int id, string SelectedEstadoRow)?

  • I changed this method to public Actionresult Alterarestafericao(Gestaoafericoesviewmodel model), and this method will then change fields in the database with data from that model, be the id of each Row, and the respective state selected in the droplist

  • in that case you will have to post the Viewmodel to it

Show 6 more comments
No answers

Browser other questions tagged

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