Rahul Patil

Rahul Patil

  • 1.5k
  • 180
  • 29.4k

How to delete a record in Asp.Net Mvc using WebApi?

Jan 11 2020 6:42 AM

I am performing asp.net mvc entity framework with webapi and when I m deleting a record but get an error.

Index.cshtml
  1. @Html.ActionLink("Delete""DPatymaster"new { PartyMstId = item.PartyMstId }) //when click delete button  
PartyMstsController (Webapi project) 
  1. namespace PartyCrops.Controllers  
  2. {  
  3.     public class PartyMstsController : ApiController  
  4.     {  
  5.         private POLISHSTOCK_SJDMSEntities db = new POLISHSTOCK_SJDMSEntities();    
  6.           
  7.         [ResponseType(typeof(PartyMst))]  
  8.         public IHttpActionResult DeletePartyMst(PartyMst partyMst)  
  9.         {  
  10.             db.PartyMsts.Find (partyMst.PartyMstId);  //when I Debugging this line generate an error Object referance not set to an object  
  11.             if (partyMst == null)  
  12.             {  
  13.                 return NotFound();  
  14.             }  
  15.   
  16.             db.PartyMsts.Remove(partyMst);  
  17.             db.SaveChanges();  
  18.   
  19.             return Ok(partyMst);  
  20.         }  
PartyMstsController (normal project)
  1. namespace WebParty.Controllers  
  2. {  
  3.     public class PartyMstsController : Controller  
  4.     {  
  5.         POLISHSTOCK_SJDMSEntities poly = new POLISHSTOCK_SJDMSEntities();    
  6.   
  7.          public ActionResult dPatymaster(int PartyMstId)  
  8.          {  
  9.             var partymst = poly.PartyMsts.Find(PartyMstId);  
  10.   
  11.             partymstmodel partymstupdate = new partymstmodel();  
  12.   
  13.             partymstupdate.PartyCode = partymst.PartyCode;  
  14.             partymstupdate.PartyName = partymst.PartyName;  
  15.   
  16.             return View(partymstupdate);  
  17.         }  
  18.   
  19.         [HttpPost]  
  20.         public JsonResult DPatymaster(partymstmodel partymst, int PartyMstId)  
  21.         {  
  22.             var partymstupdate = poly.PartyMsts.Find(PartyMstId);  
  23.   
  24.             partymstupdate.PartyCode = partymst.PartyCode;  
  25.             partymstupdate.PartyName = partymst.PartyName;  
  26.               
  27.   
  28.             HttpResponseMessage response = staticvariable.client.DeleteAsync("PartyMsts").Result; //When I m Debugging then Debugger come to this method DeletePartyMst(Api Project)  db.PartyMsts.Find (partyMst.PartyMstId); (here get an error object referance not set to an object)  
  29.             return Json("your record delete", JsonRequestBehavior.AllowGet);  
  30.         }  
PartyMst.cs
  1. //   
  2. //     This code was generated from a template.  
  3. //  
  4. //     Manual changes to this file may cause unexpected behavior in your application.  
  5. //     Manual changes to this file will be overwritten if the code is regenerated.  
  6. //   
  7. //------------------------------------------------------------------------------  
  8.   
  9. namespace PartyCrops.Models  
  10. {  
  11.     using System;  
  12.     using System.Collections.Generic;  
  13.       
  14.     public partial class PartyMst  
  15.     {   
  16.         public int PartyMstId { getset; }  
  17.         public Nullable<int> PartyCode { getset; }  
  18.         public string PartyName { getset; }  
static variable:
  1. public static class staticvariable  
  2.     {  
  3.         public static HttpClient client = new HttpClient();   
  4.   
  5.         static staticvariable()  
  6.         {  
  7.             client.BaseAddress = new Uri("http://localhost:27099/api/");  
  8.             client.DefaultRequestHeaders.Clear();  
  9.             client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));  
  10.         }  
  11.     }  
Model1.context.cs
  1. </auto-generated>    
  2. <auto-generated>    
  3. namespace PartyCrops.Models    
  4. {    
  5.     using System;    
  6.     using System.Data.Entity;    
  7.     using System.Data.Entity.Infrastructure;    
  8.         
  9.     public partial class POLISHSTOCK_SJDMSEntities : DbContext    
  10.     {    
  11.         public POLISHSTOCK_SJDMSEntities()    
  12.             : base("name=POLISHSTOCK_SJDMSEntities")    
  13.         {    
  14.         }    
  15.         
  16.         protected override void OnModelCreating(DbModelBuilder modelBuilder)    
  17.         {    
  18.             throw new UnintentionalCodeFirstException();    
  19.         }    
  20.     
  21.          public virtual DbSet<PartyMst> PartyMsts { getset; }    
when I m debugging in watch windows

Name value

PartyMstId 4 partymstcontroller.cs (mvc project)

partyMst null partymstcontroller.cs (webapi project)

db.PartyMsts.Find (partyMst.PartyMstId); here give an null value ? 

 partyMst model null when entering the contoller action
 
public int PartyMstId { get; set; } //when dubugger come here then 0  
  1. namespace WebParty.Models  
  2. {  
  3.    public class partymstmodel  
  4.    {  
  5.       public int PartyMstId { getset; }   //0 0  
 plz help???

Answers (3)