0

i have a repeating table in razor mvc , my model is an array

@model Models.Event[]

when i delete any row , every thing is ok and i check data in break point... but view can't render data correctly and always last row is deleted. in my code, i want to delete row index 2 ... i give it as hard code , in controller and view true data is passed but at the end, in view (display page) , the last row is deleted and the row with index 2 is there again :(

i pass my model from an partial view to the main page.

here is my javascript codes:

<script type="text/javascript">
        $(document)
            .ready(function () {
                $("#EventsTbl")
                    .on("click", "tbody .btn-delrow",
                        function () {
                            var deletedrow = $(this).closest("tr");
                            var index = 2; // deletedrow.attr("data-ownum");
                            var data = $("#edit-Event-form").serialize();
                            deleteEvent(data, index);
                        });

                function deleteEvent(data,index) {
                    if (confirm("Do you want to delete product: " + index)) {
                        var postdata = data;
                        $.post('/Events/DeleteDetailRow', postdata,
                        function (response) {
                            $("#EventsTbl tbody").html(response);
                        });
                    }
                }
            });
    </script>

and in controller we have :

[HttpPost]
public ActionResult DeleteDetailRow(Event[] model)
{
     var list = new List<Event>(model);
     list.RemoveAt(2);
     return PartialView("AllDetailRows", list.ToArray());
 }

"list" has true data after delete, but i don't know where is the problem. at AllDetailRows model has true data too.

here is my partial view code : (AllDetailRows)

@model Models.Event[]

@for (int i = 0; i < Model.Length; i++)
{
    <tr data-rownum="@i">
        <td class="input">
            @Html.EditorFor(model => model[i].EventExp,new {value=Model?[i]?.EventExp})
            @Html.ValidationMessageFor(model => model[i].EventExp, "", new { @class = "text-danger" })
        </td>
        <td>
            <button class="btn btn-sm btn-danger btn-delrow" type="button">
                <i class="fa fa-remove"></i>
            </button>
        </td>
    </tr>
}

and this is my main view :

@model Models.Event[]

@* This partial view defines form fields that will appear when creating and editing entities *@


<fieldset>
    <div>
        <table class="table table-bordered table-striped" id="AdverseEventsTbl">
            <thead>
                <tr>
                    <th class="text-center or-bold" style="width: 10%">@Html.LabelFor(model => model.First().EventExp)</th>
                    <th style="width: 5%">
                        <button class="btn btn-sm btn-success btn-addrow" type="button">
                            <i class="fa fa-plus-square"></i>
                        </button>
                    </th>
                </tr>
            </thead>
            <tbody>
                @Html.Partial("AllDetailRows", Model)
            </tbody>
        </table>
    </div>
</fieldset>

any idea?

thanks is advance

  • Show your view. –  Oct 09 '16 at 11:35
  • @Stephen Muecke : i updated the code :) – Bahar_Engineer Oct 09 '16 at 11:43
  • The issue is that when you submit, the values of your model are added to `ModelState` and its those values that are used by the `HtmlHelper` methods. Its not really clear what your actually trying to do with your code, but you can display the correct view by calling `ModelState.Clear();` as `return PartialView(...);` (I will find a dupe that explains this in more detail) –  Oct 09 '16 at 11:49
  • @Stephen Muecke : thanks that worked :) please post the solution as answer – Bahar_Engineer Oct 09 '16 at 11:54
  • 1
    Possible duplicate of [Incorrect List Model Binding indices when using HTML Helpers](http://stackoverflow.com/questions/35605740/incorrect-list-model-binding-indices-when-using-html-helpers) –  Oct 09 '16 at 11:55
  • The dupe explains the behavior your seeing, but there is no point postng back the whole collection in order to delete one row. You should be just posting back the ID of the item you delete, remove it from the database and return a `JsonResult` indicating success or otherwise (and if successful, remove the associated item from the DOM). And refer also [this answer](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) for dynamically adding and removing collection items –  Oct 09 '16 at 11:59
  • @ Stephen Muecke : your first answer worked correctly , i can't get id from model and remove from database because i do these steps before save in database... i do them in view ... before save – Bahar_Engineer Oct 09 '16 at 12:08
  • What steps are you referring to? (your current implementation makes no sense) –  Oct 09 '16 at 12:12

0 Answers0