Go through a table (grid) Razor MVC

2

I have a table that is populated by a Model in HTML Razor in a partialView , a field of this table I left as editable using @Html.TextBoxFor . After the user edit this field I need to update in the DB, but first I need to retrieve the changed value.

How can I go through the table and get this value in Controller ?

Here is the code for the page:

<table id="tblLivros" class="table table-hover table-striped" cellspacing="0" style="width: 100%;">
    <thead>
        <tr>
            <th>Livro</th>
            <th>Valor</th>                      
        </tr>
    </thead>
    <tbody>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Livro)
            </td>                                             
            <td>
                @Html.TextBoxFor(modelItem => item.Valor, new { style = "width: 50px;"})
            </td>
        </tr>
    }
    </tbody>
</table>
<div class="form-actions text-right pal">
    <button type="submit" class="btn btn-primary" name="Salvar" value="Salvar">
        Salvar Alterações
    </button>
    </div>

And of Controller :

[HttpPost]
public ActionResult Salvar(ViewModel Livros)
{
    //Percorrer a tabela 
}
    
asked by anonymous 12.06.2015 / 17:03

2 answers

1

I was able to retrieve the value of the grid using ajax. After the grid values are changed the user clicks the "Save Changes" button, this button executes the ajax that runs through the table and calls a function in my Controller that saves the changes in the database.

Follow the code with the solution:

<table id="tblLivros" class="table table-hover table-striped" cellspacing="0" style="width: 100%;">
<thead>
    <tr>
        <th>Livro</th>
        <th>Valor</th>                      
    </tr>
</thead>
<tbody>
@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Livro)
        </td>                                             
        <td>
            @Html.TextBoxFor(modelItem => item.Valor, new { style = "width: 50px;"})
        </td>
    </tr>
}
</tbody>

             Save editions          

ajax :

 <script type="text/javascript"> 
function Salvar() {               
            $("#tblLivros input[type=text]").each(function () {
                var valor = $(this).val();                        
                    $.ajax({
                        url: "/Livros/Salvar",
                        type: "POST",
                        data: JSON.stringify({ "valor": valor),
                        contentType: 'application/json; charset=utf-8',
                        success: function (result) {
                            // ...
                        },
                        error: function (request) {
                            // ...
                        }
                    });                
            });
  
      </script>

and no Controller :

[HttpPost]
    public ActionResult Salvar(decimal valor)
    {
        //Atualiza no DB

        return View();
    }

Personal Vlw!

    
24.06.2015 / 16:47
0

You can pass this value as a parameter.

[HttpPost]
public ActionResult Salvar(string Valor)
{
    //passe o tipo
}
    
23.06.2015 / 20:23