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 theActionLink
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 viaJavaScript
– Leandro Angelo
Right however the question I refer to is which model.Selectedestadorow supposedly obtained by Dropdownlistfor is Null when inserted into Actionlink @Model.Selectedestadorow
– Nuno Santos
How would it be easier to perform this action? Can you give me an example? Either via Post or via Javascript?
– Nuno Santos
It is null because it is running before the control is rederized on the screen
– Leandro Angelo
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
– Nuno Santos
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...
– Nuno Santos
That’s another completely different problem
– Leandro Angelo
And do you have any idea how to help me solve this problem?
– Nuno Santos
Depends, what makes the method
AlterarEstadoAfericao(int id, string SelectedEstadoRow)
?– Leandro Angelo
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
– Nuno Santos
in that case you will have to post the Viewmodel to it
– Leandro Angelo