C
C#9mo 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
Angius9mo 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
XD9mo ago
no
Angius
Angius9mo 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
XD9mo ago
Angius
Angius9mo ago
Well there's your problem, then
XD
XD9mo ago
I don't get why is the @HabitType being displayed like that though
Angius
Angius9mo 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
XD9mo ago
but if it's always the same then it's the same object all the time right?
Angius
Angius9mo 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
MODiX9mo 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
Angius9mo ago
As demonstrated
XD
XD9mo 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
Angius9mo 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
XD9mo ago
like, selecting the id and then getting the habit through the id with c#?
Angius
Angius9mo 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
XD9mo 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
Angius9mo ago
This has nothing to do with Razor Pages, but rather with relational databases
XD
XD9mo ago
yeah I'm still a beginner in sql too
Angius
Angius9mo ago
Well, google around, learn about the "relational" part of "relational database", and come back should you have more issues
Accord
Accord9mo 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
More Posts
❔ How to get substring up until a certain pattern is seen?I have a bunch of strings that look similar to: "TEST1_TEST_1995_12_324324_whatever" "test2_test_3_4❔ What is the Best Way to convert the nested C# object to key value pairI want to store the C# objects in MongoDB which can be dynamic nature in key value fashion like Attr❔ Blazor WebAsm (.NET6) Breakpoints No Longer WorkingI had them working a few months ago before I took a break from the project but now they simply don't❔ Representing a custom map using GeoJSON data instead of a map of the globe. Is it possible?I was wondering if it were possible to create a custom map not of the Earth and use GeoJSON to creat❔ RecyclableMemoryStreams and Sending a Byte ArrayI am utilizing `RecyclableMemoryStream` to pool my memory streams in my networking project, but the ❔ What is the purpose of `ModelBindingContext.Model`, `ModelName`, and `ModelBinderAttribute.Name`?The docs are very unclear about this. If I should set `ModelBindingContext.Result` to `ModelBindingR❔ System.Numerics.Vector<double> limited to 2 components with NativeAOT instead of 4Hi! Im using the mentioned Vector struct for double Vectors and have seen that using NativeAOT it on❔ SSH.Net library using in C# App questionHello all, it is no problem to connect to an ssh server and run commands and copy files to the remot❔ how do i use discord webhooks in a console app?title❔ MSSQL server is not connecting to server please help me.TITLE: Connect to Server ------------------------------ Cannot connect to DESKTOP-C2CJAPC. -------