I am performing asp.net mvc entity framework with webapi and when I m deleting a record but get an error.
Index.cshtml
- @Html.ActionLink("Delete", "DPatymaster", new { PartyMstId = item.PartyMstId }) //when click delete button
- namespace PartyCrops.Controllers
- {
- public class PartyMstsController : ApiController
- {
- private POLISHSTOCK_SJDMSEntities db = new POLISHSTOCK_SJDMSEntities();
- [ResponseType(typeof(PartyMst))]
- public IHttpActionResult DeletePartyMst(PartyMst partyMst)
- {
- db.PartyMsts.Find (partyMst.PartyMstId); //when I Debugging this line generate an error Object referance not set to an object
- if (partyMst == null)
- {
- return NotFound();
- }
- db.PartyMsts.Remove(partyMst);
- db.SaveChanges();
- return Ok(partyMst);
- }
- namespace WebParty.Controllers
- {
- public class PartyMstsController : Controller
- {
- POLISHSTOCK_SJDMSEntities poly = new POLISHSTOCK_SJDMSEntities();
- public ActionResult dPatymaster(int PartyMstId)
- {
- var partymst = poly.PartyMsts.Find(PartyMstId);
- partymstmodel partymstupdate = new partymstmodel();
- partymstupdate.PartyCode = partymst.PartyCode;
- partymstupdate.PartyName = partymst.PartyName;
- return View(partymstupdate);
- }
- [HttpPost]
- public JsonResult DPatymaster(partymstmodel partymst, int PartyMstId)
- {
- var partymstupdate = poly.PartyMsts.Find(PartyMstId);
- partymstupdate.PartyCode = partymst.PartyCode;
- partymstupdate.PartyName = partymst.PartyName;
- 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)
- return Json("your record delete", JsonRequestBehavior.AllowGet);
- }
PartyMst.cs
- //
- // This code was generated from a template.
- //
- // Manual changes to this file may cause unexpected behavior in your application.
- // Manual changes to this file will be overwritten if the code is regenerated.
- //
- //------------------------------------------------------------------------------
- namespace PartyCrops.Models
- {
- using System;
- using System.Collections.Generic;
- public partial class PartyMst
- {
- public int PartyMstId { get; set; }
- public Nullable<int> PartyCode { get; set; }
- public string PartyName { get; set; }
- public static class staticvariable
- {
- public static HttpClient client = new HttpClient();
- static staticvariable()
- {
- client.BaseAddress = new Uri("http://localhost:27099/api/");
- client.DefaultRequestHeaders.Clear();
- client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
- }
- }
- namespace PartyCrops.Models
- {
- using System;
- using System.Data.Entity;
- using System.Data.Entity.Infrastructure;
- public partial class POLISHSTOCK_SJDMSEntities : DbContext
- {
- public POLISHSTOCK_SJDMSEntities()
- : base("name=POLISHSTOCK_SJDMSEntities")
- {
- }
- protected override void OnModelCreating(DbModelBuilder modelBuilder)
- {
- throw new UnintentionalCodeFirstException();
- }
- public virtual DbSet
PartyMsts { get; set; }
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
- namespace WebParty.Models
- {
- public class partymstmodel
- {
- public int PartyMstId { get; set; } //0 0
plz help???
Rahul PatilPosted Jan 15, 2020, 10:09 AM
Jignesh KumarPosted Jan 11, 2020, 9:12 PM
Salman BegPosted Jan 11, 2020, 8:14 AM