0

I'm trying to display a table in razor that has delete buttons on each row and there is an add button in the toolbar. When I delete an item it calls an ajax POST that will remove the item from the list(model).

//Ajax
//The add function is basically the same
   function DeleteFromGrid(index) {
            var formData = $("#GridForm").serialize();
            $.ajax({
                type: "POST",
                cache: false,
                url: "People/DeleteFromGrid?index=" + index,
                data: formData,
                success: function (data) {
                    $("#Container").html(data);
                }
            });
        }

//Controller
//The add function is very similar is just adds an item
    Public Function DeleteFromGrid(viewModel As PeopleViewModel, index As Integer) As ActionResult    
        viewModel.People.RemoveAt(index)
        Return PartialView("PeopleView", viewModel)
    End Function


//Html-Razor
<table id="MultiRateGrid" class="DataGrid" style="width: 100%">
     <tbody>
        <tr> 
            <th>@Html.LabelFor(Function(model) model.People(0).Name)</th>    
        </tr>

        @For i = 0 To Model.People.Count - 1
            Dim j As Integer = i
            @<tr>
                <td>
                    @Html.TextBoxFor(Function(m) m.People(j).Name)    
                    @Model.People(j).Name
                </td>
                <td>
                    <input type="button" onclick="@String.Format("DeleteFromGrid(({0})", j)" value="Delete"/>
                </td>
            </tr>
        Next    

    </tbody>
</table>

My issue is if I delete the first item from the list, the viewModel is updated correctly by me checking with @Model.People(j).Name (and in the controller) but actually displays the old value in the TextboxFor. I think it might be something with binding due to it retaining the previous object bound to People(j) because it is a lambda.

Leonardo
  • 183
  • 2
  • 6
  • You can call `ModelState.Clear()` before returning the view (but your taking the wrong approach here (posting back the whole collection and then sending it back again) –  Sep 19 '16 at 11:11
  • yes Stephen is right, just pass Unique/PK column and delete based on that – Sandip - Frontend Developer Sep 19 '16 at 11:12
  • I have added in and still no luck. – Leonardo Sep 19 '16 at 11:19
  • I already had this previously as well – Leonardo Sep 19 '16 at 11:20
  • Add the hidden input for the indexer, but in callback, remove the item from the DOM. (what your currently doing is worse performance that making a normal submit to a Delete method and redirect to display the view again - there is no point using ajax) –  Sep 19 '16 at 11:21
  • Thanks a bunch guys, `ModelSate.Clear` fixed the issue. I do realise this isnt the best approach it was originally a webforms page and they do other things inbetween all postbacks so rather tha refactoring the entire page I just made do. – Leonardo Sep 19 '16 at 11:26
  • For a detailed explanation of the behavior - refer [this answer](http://stackoverflow.com/questions/37730148/rendered-partial-view-does-not-match-model/37739240#37739240). But you need to change your approach :) –  Sep 19 '16 at 11:31
  • The approach in the refered answer is the normal way I do grids :) cheers for the detail explaination in that answer it all becomes clearer now! – Leonardo Sep 19 '16 at 11:35

0 Answers0