Gabriel Lemos
Gabriel Lemos

Reputation: 75

Row to send a List<Object> from Razor page to Update?

I need to send a List back to my controller and update the values on my repository.

But after load the view with the values, update it and click the submit button, I don't know how to get the list with the update values and call the update method from the repository.

I'm using .Net 4.7.2.

HomeController:

[HttpGet]
public ActionResult Nota(string Concurso)
{
    List<InscricoesModel> model = new List<InscricoesModel>();

    InscricoesRepository repository = new InscricoesRepository();

    model = repository.GetAprovadosPrimeiraFase(new Guid(Concurso));

    return View("Nota",model);
}

[HttpPost]
public void UdateNotas(List<InscricoesModel> model)
{
    InscricoesRepository repository = new InscricoesRepository();
    foreach(InscricoesModel item in model)
    {
        repository.Update(item);
    }
}

Nota.cshtml:

@model List<ConcursoBolsaSegundaFase.Model.InscricoesModel>

<h1>Classificados 2ª Fase</h1>
<hr />
<p>Exportado em @DateTime.Now</p>

<div style="margin-top:15px">

    @* TABELA RESULTADO *@
    <div id="notasAprovadosSegundaFase" style="margin-top:10px">

        @using (Html.BeginForm("UdateNotas", "Home", Model, FormMethod.Post))
        {

            <table class="table table-bordered" id="tblNotaAprovadosSegundaFase">
                <thead>
                    <tr>
                        <th>Inscrição</th>
                        <th>Nome</th>
                        <th>Nota Primeira Fase</th>
                        <th>Fez Carta</th>
                        <th>Nota Segunda Fase</th>
                    </tr>
                </thead>
                <tbody>
                    @if (Model != null)
                    {
                        foreach (var linha in Model)
                        {
                            <tr>
                                <td>@linha.Inscricao</td>
                                <td>@linha.Nome</td>
                                <td>@linha.NotaPrimeiraFase</td>
                                <td>
                                    <select>
                                        <option value="false">Não</option>
                                        <option value="true">Sim</option>
                                    </select>
                                </td>
                                <td><input type="text" value="@linha.NotaSegundaFase"></td>
                            </tr>
                        }
                    }
                </tbody>
            </table>

            <button type="submit" class="btn btn-success">Salvar</button>

        }
    </div>
</div>

The UpdateNotas method in my controller never recive a value, i don't know how to send the list from the view to my controller.

Upvotes: 2

Views: 1455

Answers (3)

Javid Gahramanov
Javid Gahramanov

Reputation: 112

As far as I understood,you want to post DropDownList value only.

if you decide to update other fields just add the Html.Editor into the loop like following.

 @Html.Editor("[" + i + "].Inscricao")
 @Html.Editor("[" + i + "].Nome")

I hope this will help.

 @using (Html.BeginForm("UdateNotas", "Home", Model, FormMethod.Post))
    {

        <table class="table table-bordered" id="tblNotaAprovadosSegundaFase">
            <thead>
                <tr>
                    <th>Inscrição</th>
                    <th>Nome</th>
                    <th>Nota Primeira Fase</th>
                    <th>Fez Carta</th>
                    <th>Nota Segunda Fase</th>
                </tr>
            </thead>
            <tbody>
                @if (Model != null)
                {
                    @for (int i = 0; i < Model.Count; i++)
                      {
                        <tr>
                            <td>@Model[i].Inscrição</td>
                            <td>@Model[i].Nome</td>
                            <td>@Model[i].NotaPrimeiraFase</td>
                            <td>
                                <select name="[@i].NotaSegundaFase"> 
                                    <option value="false">Não</option>
                                    <option value="true">Sim</option>
                                </select>
                            </td>
                            <td><input type="text" value="@linha.NotaSegundaFase"></td>
                        </tr>
                    }
                }
            </tbody>
        </table>

        <button type="submit" class="btn btn-success">Salvar</button>

    }

Note : don't change anything in the controller. model binder should resolve everything from the request.

Upvotes: 0

User_4373
User_4373

Reputation: 297

You can use @Html.HiddenFor() to hold the data on the view and bind to controller on Post.

@using (Html.BeginForm("UdateNotas", "Home", Model, FormMethod.Post))
{
    <table class="table table-striped table-bordered table-sm table-responsive">
        <thead>
            <tr>
                <th>Inscrição</th>
                <th>Nome</th>
                <th>Nota Primeira Fase</th>
                <th>Fez Carta</th>
                <th>Nota Segunda Fase</th>

            </tr>
        </thead>
        <tbody>

            @for (int i = 0; i < Model.Count; i++)
            {
                @Html.HiddenFor(model => Model[i].Inscrição)
                @Html.HiddenFor(model => Model[i].Nome)
                @Html.HiddenFor(model => Model[i].NotaPrimeiraFase)

                <tr>
                    <td>@Model[i].Inscrição</td>
                    <td>@Model[i].Nome</td>
                    <td>@Model[i].NotaPrimeiraFase</td>
                    @*do this similary for other property *@ 
                </tr>

            }
        </tbody>
    </table>
    <button type="submit" class="btn btn-success">Salvar</button>
}

Upvotes: 0

the_lotus
the_lotus

Reputation: 12748

In MVC, the name of the input will be binded to the variable in the controller. In your case, your input has no name. I suggest you look at the html helpers.

This would bind your values properly.

for (int i = 0;i<Model.Count;i++)
{
    Html.TextBoxFor(model => Model[i].NotaSegundaFase)
}

In this case, only NotaSegundaFase would be sent back to the controller.

Upvotes: 1

Related Questions