C
C#15mo ago
XD

❔ Why isn't my form being submitted?

As far as I can see, my form is not being submitted because the HabitType is null (there must be something wrong with the section and option tags but I can't tell) Basically, the user is supposed to log their habits through a form which will insert the values into the database. Select and option tags will make the user choose which kind of habit he will log (the HabitLog class contains a HabitType property), the foreach loop will loop through each habit type which exists. The other two ones are for the user to log the date and quantity. Appreciate any help! LogHabit.cshtml: https://pastebin.com/CFusfz41 LogHabit.cshtmlcs: https://pastebin.com/JED3jAMP HabitLog class: https://pastebin.com/CJ1HK88K HabitType class: https://pastebin.com/Ly8Qtg0i
Pastebin
@page@model MVC.HabitTracker.Pages.LogHabitModel@{}Log a habit
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.
Pastebin
using Microsoft.AspNetCore.Mvc;using Microsoft.AspNetCore.Mvc.Razor...
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.
Pastebin
using System.ComponentModel.DataAnnotations;namespace MVC.HabitTrac...
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.
Pastebin
using System.ComponentModel.DataAnnotations;namespace MVC.HabitTrac...
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.
20 Replies
Angius
Angius15mo ago
Any errors? Also, you seem to be inserting everything into the habit_logs table. Shouldn't there be a foreign key to habit_types or some such? I guess we'll never know Cya
XD
XDOP15mo ago
no
Angius
Angius15mo ago
@foreach (var habitType in Model.HabitTypes)
{
<option value="@habitType">@habitType.Name</option>
}
@foreach (var habitType in Model.HabitTypes)
{
<option value="@habitType">@habitType.Name</option>
}
I don't think you have an implicit conversion operator from HabitType to any integer or anything Make sure the generated HTML actually looks correct
XD
XDOP15mo ago
Angius
Angius15mo ago
Well there's your problem, then
XD
XDOP15mo ago
I don't get why is the @HabitType being displayed like that though
Angius
Angius15mo ago
Because that's how types are displayed by default As their fully-qualified name That's the default behaviour of ToString() method of the Object class
XD
XDOP15mo ago
but if it's always the same then it's the same object all the time right?
Angius
Angius15mo ago
So you either need to override that behaviour Or use the ID or something as your value Yes, it's dependent on the type, not the given instance
MODiX
MODiX15mo ago
Angius
REPL Result: Success
class Thing{}
var arr = new Thing[]{ new(), new(), new(), new(), new() };
foreach (var a in arr) Console.WriteLine(a);
class Thing{}
var arr = new Thing[]{ new(), new(), new(), new(), new() };
foreach (var a in arr) Console.WriteLine(a);
Console Output
Submission#0+Thing
Submission#0+Thing
Submission#0+Thing
Submission#0+Thing
Submission#0+Thing
Submission#0+Thing
Submission#0+Thing
Submission#0+Thing
Submission#0+Thing
Submission#0+Thing
Compile: 624.743ms | Execution: 53.882ms | React with ❌ to remove this embed.
Angius
Angius15mo ago
As demonstrated
XD
XDOP15mo ago
what would you advice me to do if I want to select the object (habitType) to use it later like this? tableCmd.CommandText = @$"INSERT INTO habit_logs(ImagePath, Name, UnitOfMeasurement, Date, Quantity) VALUES('{HabitLog.HabitType.ImagePath}', '{HabitLog.HabitType.Name}', '{HabitLog.HabitType.UnitOfMeasurement}', '{HabitLog.Date}', {HabitLog.Quantity})";
Angius
Angius15mo ago
Well, the value of the option should be the thing you will want to use If you were to actually use the "relational" part of "relational database", an ID would be what you want
XD
XDOP15mo ago
like, selecting the id and then getting the habit through the id with c#?
Angius
Angius15mo ago
Like getting the ID and saving it in the habit, as a foreign key to habit type A classic one-to-many relationship
XD
XDOP15mo ago
alright, will try bro, thank you for your help and sorry if i have a bit of trouble understanding you, I'm new to razor pages
Angius
Angius15mo ago
This has nothing to do with Razor Pages, but rather with relational databases
XD
XDOP15mo ago
yeah I'm still a beginner in sql too
Angius
Angius15mo ago
Well, google around, learn about the "relational" part of "relational database", and come back should you have more issues
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server