morry329#
Id points to always null
Could you kindly point me what went wrong here .. I mean I tried to map the controller to the cshtml but the id is still null at my controller
My controller https://pastebin.com/cSzKufsU
My cshtml https://pastebin.com/rwqSwMV2 <--- I even created a form tag to mark the form field with the same Id in controller. Still none of these helped
7 replies
The edit method does not edit the pre-existing data but it creates a new data entry with NULL
So my rider does not let me set any breakpoint in code (the point will get marked by 🚫 ), I did something like this as debug
[HttpGet]
public JsonResult EditListingJ(int? Id) //For getting details of the selected User
{
try
{
ListingProjectsDTO StartEditListing = _context.ListingDTO_DBTable.Find(Id);
Console.WriteLine("GET success");
return Json(StartEditListing);
}
catch (Exception e)
{
return null;
}
}
[HttpPost]
public JsonResult EditListingJ([FromForm]ListingProjectsDTO _completeEditNowListingProjectsDto)
{
try
{
_context.ListingDTO_DBTable.Update(_completeEditNowListingProjectsDto);
_context.SaveChanges();
Console.WriteLine("POST success");
return Json(_completeEditNowListingProjectsDto);
}
catch (Exception ex)
{
return Json(ex.Message);
}
}
This code returns both of Console.WriteLine("GET success") and Console.WriteLine("POST success");26 replies
The edit method does not edit the pre-existing data but it creates a new data entry with NULL
So my entity is in the added state as it has no primary key set (henceforth my app adds a new entry instead of editing the pre-existing one). Did I understand the doc correctly?
26 replies
✅ NullPointException in the Update/Post method, but not in the Get Method
Some weeks ago, my code needed to check if the HTTP request would hit the controller action while bypassing the frontend. So I forgot to remove that 'remnant' from this experiement
46 replies
✅ NullPointException in the Update/Post method, but not in the Get Method
ListingProjectsDTO
public class ListingProjectsDTO
{
[System.ComponentModel.DataAnnotations.Key]
public int? Id { get; set; } // Primary key for DTO
public string? ListingName { get; set; } // Mapped from ListingName in ListingProjects
}
getListingProjectsDto is not a class but a part of interface
public interface IListingProjectsDtoRepository
{
ListingProjectsDTO getListingProjectsDto(int? Id);
IEnumerable<ListingProjectsDTO> GetAllEmployee();
}
So it explains pretty much why it gives me the nullpointException then46 replies