C
C#16mo 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
Angius16mo 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
XDOP16mo ago
no
Angius
Angius16mo 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
XDOP16mo ago
Angius
Angius16mo ago
Well there's your problem, then
XD
XDOP16mo ago
I don't get why is the @HabitType being displayed like that though
Angius
Angius16mo 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
XDOP16mo ago
but if it's always the same then it's the same object all the time right?
Angius
Angius16mo 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
MODiX16mo 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
Angius16mo ago
As demonstrated
XD
XDOP16mo 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
Angius16mo 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
XDOP16mo ago
like, selecting the id and then getting the habit through the id with c#?
Angius
Angius16mo 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
XDOP16mo 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
Angius16mo ago
This has nothing to do with Razor Pages, but rather with relational databases
XD
XDOP16mo ago
yeah I'm still a beginner in sql too
Angius
Angius16mo ago
Well, google around, learn about the "relational" part of "relational database", and come back should you have more issues
Accord
Accord16mo 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.

Did you find this page helpful?