Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a doubt in mvc that how can I delete multiple entries from a table by passing id. that is to group different entries of postname under the same name and when deleting by hiting on the delete link it will show the confirmation page to delete. In the HttpGet method of delete I have written the code and will work
public async Task<ActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Mapping mapping = await db.Mappings.FindAsync(id);

            var ListItems = (from ap in db.Mappings
                             join a in db.Masters on ap.MasterId equals a.ID
                             join p in db.PostMasters on ap.PostId equals p.Id
                             select new MappingList
                             {
                                 id = ap.Id,
                                 Masterid = ap.MasterId,
                                 postid = ap.PostId,
                                 firstname = a.FirstName,
                                 Postname = p.PostName
                             }).Where(r => r.Masterid == id).ToList();

            var grp = ListItems.GroupBy(r => r.Masterid).Select(r => new MappingList
            {
                Masterid = r.Key,
                id = r.Key,
                PostNames = string.Join(" , ", r.Select(g => g.Postname)),
                firstname = ListItems.FirstOrDefault(q => q.Masterid == r.Key).firstname
            }).ToList();


            if (grp == null)
            {
                return HttpNotFound();
            }
            return View(grp);
        }
Here Mapping is the table and wants to delete entries from this table.MappingList is a modelclass.
In the delete view page have given
@model IEnumerable<A.Models.MappingList>
and get the result I need . But when I click on the delete confirm button I need to remove value of grp, so that the entry will get removed and save to Mapping table. this is the section that I am not able to do
[HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> DeleteConfirmed(int id)
        {
            Mapping mapping = await db.Mappings.FindAsync(id);
            db.Mappings.Remove(mapping);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

I am not getting value mapping because the moel passed to delete view page is not the table but a class and the id in actionparameter is the master id . Iam not able to delete the entry shows the error value cannot be null
Can anyone please help me to find the solution how to delete the entry in Mapping table by passing id ??

What I have tried:

How to delete from table when the model is different have searched many ways
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900