04May
image

This article will cover Create, Read, update and delete operation for child table 

As in previous articles we see how we can add data in two tables without using much custom JQuery or java script code for validation and we don’t have to convert our form into JSON. In this part we will see how will update that inserted data. If you don’t read previous articles, please go through.

Part 1

Part 2

Note: this will be last part of this topic.

In previous articles we have seen how we use model binding and jquery validator for Multiple models. This article is not much change from last one if we check Edit view code its look like same as this has same function and html but only difference is in edit view we have a foreach loop.

As before updating or deleting any data we need to read and show to user therefore this loop comes in and create html for Details which is already in Database.



@model Order

<form asp-action="Edit" asp-controller="Home" method="post">

    <div class="form-group">

        <label for="txtCustomer">Customer Name</label>

        <input type="text" class="form-control" id="txtCustomer" aria-describedby="emailHelp" placeholder="Customer Name" asp-for=CustomerName>

        <input type="hidden" value="@DateTime.Now" asp-for=Date />

        <input type="hidden" asp-for=Id />

        <span class="txt-danger" asp-validation-for=CustomerName></span>

    </div>

    <div class="form-group">

        <label for="txtTotal">Total</label>

        <input type="text" class="form-control" id="txtTotal" placeholder="Total Order Amount" asp-for=Total>

        <span class="txt-danger" asp-validation-for=Total></span>

    </div>

    <hr class="col-md-12" />

    <div id="content-hldr">

        @for (int i = 0; i < Model.Details.Count; i++)

        {

            <div class="row cln-det">

                <div class="col-md-5">

                    <label>Product Name</label>

                    <input type="text" class="form-control cntrl" aria-describedby="emailHelp" placeholder="Prioduct Name" asp-for=Details[i].ProductName>

                    <input type="hidden" class="cntrl" asp-for=Details[i].Id />

                    <span class="txt-danger valid-cntrl" asp-validation-for=Details[i].ProductName></span>

                </div>

                <div class="col-md-5">

                    <label>Price</label>

                    <input type="text" class="form-control cntrl" aria-describedby="emailHelp" placeholder="Total Price" asp-for=Details[i].Price>

                    <span class="txt-danger valid-cntrl" asp-validation-for=Details[i].Price></span>

                </div>

                <div class="col-md-2">

                    <div>&nbsp;</div>

                    <button type="button" class="btn btn-primary btn-add-new">Add</button>

                    <button type="button" class="btn btn-danger btn-del">Del</button>

                </div>

            </div>

        }

    </div>

    <hr class="col-md-12" />

    <button type="submit" class="btn btn-primary">Submit</button>

</form>

Remaining code is same at view now there is a problem how our Edit Post method will know if there is any data is deleted, updated or created.

Let’s we are adding new product in our order means we will add new Detail object as we know that if record is new so Id of detail will be 0. In case we are updating then Id will hold any value grater then 0. Now if we are deleting any record we will not receive that deleted record in Edit POST method so we will compare received Id,s with DB and will sync. Let’s see in code .

[HttpPost]

        public async Task<IActionResult> Edit(Order model)

        {

            var oldModel = _appDbContext.Orders.Find(model.Id);

            oldModel.Details = _appDbContext.OrdersDetails.Where(x => x.OrderId == model.Id).ToList();

            oldModel.Total = model.Total;

            oldModel.CustomerName = model.CustomerName;

            foreach (var item in model.Details)

            {

                if (item.Id == 0)

                {

                    oldModel.Details.Add(item);

                }

                else

                {

                    var oldDet = oldModel.Details.Find(x => x.Id == item.Id);

                    if (oldDet?.Id > 0)

                    {

                        oldDet.ProductName = item.ProductName;

                        oldDet.Price = item.Price;

                    }

                }

            }

           var deletedDet = oldModel.Details.Where(x => !model.Details.Select(y => y.Id).ToList().Contains(x.Id)).ToList();

            foreach (var item in deletedDet)

            {

                    _appDbContext.OrdersDetails.Remove(item);

            }

            await _appDbContext.SaveChanges();

            return View(model);

        }

You can download all code from github.

Comments / 1

image
Williamduh

Hello, Download MP3/FLAC FTP full access to exclusive electronic music. https://0daymusic.org Private FTP Music/Albums/mp3 1990-2022 List: https://0daymusic.org/FTPtxt/ Best regards, 0daymusic Team.

Post A Comment