C
C#4w ago
morry329#

Save button does not save the inserted data

So I have created a save button in my insert view as per screenshot The code behind it does not save as I wanted The view lets me insert any value but the button does not save it onclick. Once clicked there is nothing happening there, even no error messages. My code here https://pastebin.com/7LyeZxtt Could anyone kindly point me in the right direction?
Pastebin
/Controller/ [AllowAnonymous] public IActionResult InsertListi...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
No description
9 Replies
Sossenbinder
Sossenbinder4w ago
Did you debug this already? Did the request even reach your server? Did you check whether the stored object is valid?
✿ Scarlet ✿
@Sowehaoiu77 Try changing to:
<input type="submit" style="float:right;" data-entity-id="@Model.Id" class="btn btn-info" value="Save" />
<input type="submit" style="float:right;" data-entity-id="@Model.Id" class="btn btn-info" value="Save" />
morry329#
morry329#OP4w ago
Network tab just got me this https://pastebin.com/stiTYN5g This exception seems to point to
[HttpGet("availableListings")]
public async Task<IEnumerable<ListingProjects>> Get()
{
return await _context.ListingDBTable.ToListAsync();
}
[HttpGet("availableListings")]
public async Task<IEnumerable<ListingProjects>> Get()
{
return await _context.ListingDBTable.ToListAsync();
}
` This method is in the API controller class separately created from HomeController (where this save method lies) : you can find the full code for the API controller https://pastebin.com/f73x8fDN I tried something like this on Program.cs
builder.Services.AddControllersWithViews().AddJsonOptions(options =>
{
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve;
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
});
builder.Services.AddControllersWithViews().AddJsonOptions(options =>
{
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve;
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
});
` But the exception is not gone I suppose this exception prevents my code from what it should be doing
Pastebin
System.Text.Json.JsonException: A possible object cycle was detecte...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
✿ Scarlet ✿
you are returning all records from the table?
morry329#
morry329#OP4w ago
ups, I did not want to return all the records from the table .. see, I did not understand what my code is doing. so with this line return await _context.ListingDBTable.ToListAsync(); it will return all records from the table, correct? And it eventually triggers the exception as it causes the circulation or
Sossenbinder
Sossenbinder4w ago
Yes, it does - But your issue is with the IEnumerable implementation, causing an endless cycle What's the reason for the ListingProjects to be enumerable if it only contains one entry?
morry329#
morry329#OP4w ago
I just got rid of all the IEnumerable implementation, then the exception is gone. ChatGPT recommended me to make the ListingProjects enumerable (as I wanted to make a collection of vacation listing names) But now for the sake of simplicity I will learn more about IEnumerable, this one caused me some unwanted exception
Sossenbinder
Sossenbinder4w ago
Sounds good 👍
Jimmacle
Jimmacle4w ago
you should not use EF entities as DTOs that's why you're getting an exception about object cycles EF entities with navigation properties can have cyclical references which causes problems when serializing to JSON
Want results from more Discord servers?
Add your server