0

In the Edit method of a controller, I cannot successfully validate and pass the details of a model. But when I add new row, it validates and pass it's values to the controller.

Here is the sample output:

enter image description here

The two records is not passing and validating but when I add a new row, the record from new row validates and passes

enter image description here

Here is my code for getting the existing details

var form = $('form');
        $.ajax({
            url: '/Journals/EditJournalDetails',
            data: {
                id: @Model.Id
            },
            success: function (data) {
                $('#journalRow').append('<tr>' + data + '</tr>');
                $('tbody#journalRow>tr.checkDetails').appendTo('#checkRow');
                form.data('validator', null);
                $.validator.unobtrusive.parse(form);
            }
        });

the code for EditJournalDetails

    @model IEnumerable<SimplyAccounting.Models.TransactionViewModels.JournalDetailsViewModel>
@using (Html.BeginCollectionItem("JournalDetailsViewModel"))
{
    @foreach (var item in Model)
    {
//row values here
}
}

Controller code:

    public async Task<IActionResult> EditJournalDetails(int? id)
        {
            var journaldetails = await _context.JournalDetails.Where(m => m.JournalId == id).ToListAsync();
            var jdvmodel = _mapper.Map<List<JournalDetailsViewModel>>(journaldetails);
            foreach(var item in jdvmodel)
            {
                //retrieve data here
}
            return PartialView("_EditJournalDetails", jdvmodel);
        }

Add row code JS:

function GetRow() {
        var form = $('form');
        $.ajax({
            url: '/Journals/CreateJournalDetails',
            success: function (data) {
                $('#journalRow').append('<tr>' + data + '</tr>');
                $('tbody#journalRow>tr.checkDetails').appendTo('#checkRow');
                form.data('validator', null);
                $.validator.unobtrusive.parse(form);
            }
        });
    }

CreateJournalDetails partialview code:

 @model SimplyAccounting.Models.TransactionViewModels.JournalDetailsViewModel
@using (Html.BeginCollectionItem("JournalDetailsViewModel"))
{
//row codes here
}

Controller code:

public IActionResult CreateJournalDetails(JournalDetailsViewModel vmodel)
    {
        vmodel = new JournalDetailsViewModel();
        //some code here
        return PartialView("_CreateJournalDetails", vmodel);
    }
Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
  • 1
    Your `EditJournalDetails` code is not correct. You do not put a loop inside `BeginCollectionItem`. You have `foreach` loop that calls `@Html.Partial` and the partial view is based on a single model and has a `BeginCollectionItem`. refer [A Partial View passing a collection using the Html.BeginCollectionItem helper](https://stackoverflow.com/questions/40539321/a-partial-view-passing-a-collection-using-the-html-begincollectionitem-helper/40541892#40541892) for the detailed code. And why are you making an ajax call to get the existing records? –  Oct 11 '18 at 08:01

1 Answers1

0

Solve the issues using this code

View

<tbody id="journalRow">
                                        @foreach(var item in Model.JournalDetailsViewModel)
                                        {
                                            @Html.Partial("_JournalDetails", item)
                                        }
                                    </tbody>

PartialView

  @model SimplyAccounting.Models.TransactionViewModels.JournalDetailsViewModel
@using (Html.BeginCollectionItem("JournalDetailsViewModel"))
{
    <tr class="checkDetails" id="@Model.Guid">
        <td>
}
}