'How do I get the userId to save in the database while submitting the form?

I'm a beginner and well i'm doing a form which is capable to send information to the database but I needed one more thing to be sent and it is the Id from the user who submit. I'm using identity framework I didn't change at all the template code. My Submit controller was done using scaffold. Can you guys explain what am I doing wrong? I tried to search in every corner of internet but I didn't get the answer for my question maybe it's too obvious and didn't notice at all.

The code is right below.

.NET CORE VERSION 3.1


Form.cshtml

``` 
@page
@model AuthSystem.Areas.Identity.Pages.Form.FormModel

@{
    ViewData["Title"] = "Form";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<center>
    <h1>Form</h1>

    <h4>Submit</h4>
</center>
<hr />
<div class="row">
    <div class="col-md-4">
        <form method="post" class="row">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>

            <div class="row">
                <div class="form-group">
                    <label asp-for="Submit.NrOperador" class="control-label"></label>
                    <input asp-for="Submit.NrOperador" class="form-control" />
                    <span asp-validation-for="Submit.NrOperador" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="Submit.Posto" class="control-label"></label>
                    <input asp-for="Submit.Posto" class="form-control" />
                    <span asp-validation-for="Submit.Posto" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="Submit.Maquina" class="control-label"></label>
                    <input asp-for="Submit.Maquina" class="form-control" />
                    <span asp-validation-for="Submit.Maquina" class="text-danger"></span>
                </div>
            </div>
            <div class="row">
                <div class="form-group">
                    <label type="time" asp-for="Submit.Hora" class="control-label"></label>
                    <input type="time" asp-for="Submit.Hora" class="form-control" />
                    <span asp-validation-for="Submit.Hora" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="Submit.Date" class="control-label"></label>
                    <input type="date" asp-for="Submit.Date" class="form-control" />
                    <span asp-validation-for="Submit.Date" class="text-danger"></span>
                </div>
            </div>


            <div class="row">
                <div class="form-group">
                    <label asp-for="Submit.Item122" class="control-label"></label>
                    <input asp-for="Submit.Item122" class="form-control" />
                    <span asp-validation-for="Submit.Item122" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="Submit.Item132" class="control-label"></label>
                    <input asp-for="Submit.Item132" class="form-control" />
                    <span asp-validation-for="Submit.Item132" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="Submit.Item212" class="control-label"></label>
                    <input asp-for="Submit.Item212" class="form-control" />
                    <span asp-validation-for="Submit.Item212" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="Submit.Caract"></label>
                    <br />
                    <input type="radio" value="Sim" asp-for="Submit.Caract" class="custom-radio" />
                    <a> Sim </a>
                    <br />
                    <input type="radio" value="Não" asp-for="Submit.Caract" class="custom-radio" />
                    <a> Não </a>
                    <span asp-validation-for="Submit.Caract" class="text-danger"></span>

                </div>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
      
    </div>
</div>

<div>
    <a asp-page="./Areas/Identity/Pages/Form/FormList">Back to List</a>
</div>

Form.cshtml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using AuthSystem.Models;

namespace AuthSystem.Areas.Identity.Pages.Form
{
    public class FormModel : PageModel
    {

        private readonly AuthSystem.Models.FormContext _context;

        public FormModel(AuthSystem.Models.FormContext context)
        {
            _context = context;
        }

        public IActionResult OnGet()
        {
            return Page();
        }


        [BindProperty]
        public Submit Submit { get; set; }

        // To protect from overposting attacks, enable the specific properties you want to bind to, for
        // more details, see https://aka.ms/RazorPagesCRUD.
        public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {

                return Page();
            }

            _context.Submit.Add(Submit);
            await _context.SaveChangesAsync();

            return RedirectToPage("./Index");
        }
    }
}


Solution 1:[1]

I'm not sure if I understood your issue. But I think you should using Sessions Collection to catch userId. I assume you have model with property UserId type of string after success login make Session["UserId"]

if (!ModelState.IsValid)
{
    return Page();
}


model.UserId = Convert.ToString(Session["UserId"]);

Add it before _Context.SaveChanges()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SomeBody