在提交表单时如何获得userId以保存在数据库中?



我是一个初学者,我正在做一个能够将信息发送到数据库的表单,但我需要再发送一件事,它是从用户提交的Id。我使用身份框架,我没有改变所有的模板代码。我的Submit控制器是使用scaffold完成的。你们能解释一下我哪里做错了吗?我试着在互联网的每个角落搜索,但我没有得到我的问题的答案,也许它太明显了,我根本没有注意到。

代码如下:

。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");
}
}
}
<代码>

我不确定我是否理解了你的问题。但是我认为你应该使用Sessions Collection来捕捉userId。我假设你有一个模型,属性UserId类型的字符串成功登录后,使会话["UserId"]

PP_4加在CC_2前

相关内容

  • 没有找到相关文章

最新更新