我正在制作一個專案,我需要創建一個表單來填充資料庫。
我可以使用 Asp-for 處理程式來填寫表單并且它正在作業。
但是我想在幕后添加一些資訊,例如用戶和創建日期。
我想做這樣的事情:
public DateTime CurrentDate = DateTime.Now;
ModelForClients.CreationDate = CurrentDate; //That is what I would like to do.
ModelForClients.UserType = currentUser // User which is filling the form I'm using login with Identity framework
你能告訴我如何從頁面模型中添加資訊嗎?
模型頁面 (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 ArchiProjectManager.Data;
using ArchiProjectManager.Models;
using Microsoft.AspNetCore.Identity;
namespace ArchiProjectManager.Pages.Users.Clients
{
public class CreateModel : PageModel
{
private readonly ArchiProjectManager.Data.ApplicationDbContext _context;
public CreateModel(ArchiProjectManager.Data.ApplicationDbContext context)
{
_context = context;
}
public IActionResult OnGet()
{
return Page();
}
[BindProperty]
public ModelForClients ModelForClients {
get; set;
}
//Variables added for the form
public DateTime CurrentDate = DateTime.Now;
ModelForClients.CreationDate = CurrentDate; //That is what I would like to do.
ModelForClients.UserType = currentUser // User which is filling the form I'm using login with Identity framework
//Need to continue to add the user name and current date for the creation
// To protect from overposting attacks, see https://aka.ms/RazorPagesCRUD
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
_context.ModelForClients.Add(ModelForClients);
await _context.SaveChangesAsync();
return RedirectToPage("./Clients");
}
}
}
前視圖(剃刀頁面)
@page
@model CreateModel
@{
ViewData["Title"] = "Create";
}
<div class="adminContainer">
<div>
<a asp-page="/Users/Clients/Clients" class="btn btn-danger">
<i class="bi bi-backspace-fill"></i>
</a>
</div>
<h1>Create</h1>
<h4>ModelForClients</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="ModelForClients.LastName" class="control-label"></label>
<input asp-for="ModelForClients.LastName" class="form-control" />
<span asp-validation-for="ModelForClients.LastName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ModelForClients.FirstName" class="control-label"></label>
<input asp-for="ModelForClients.FirstName" class="form-control" />
<span asp-validation-for="ModelForClients.FirstName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ModelForClients.Adress" class="control-label"></label>
<input asp-for="ModelForClients.Adress" class="form-control" />
<span asp-validation-for="ModelForClients.Adress" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ModelForClients.Company" class="control-label"></label>
<input asp-for="ModelForClients.Company" class="form-control" />
<span asp-validation-for="ModelForClients.Company" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ModelForClients.LegalRepresentative" class="control-label"></label>
<input asp-for="ModelForClients.LegalRepresentative" class="form-control" />
<span asp-validation-for="ModelForClients.LegalRepresentative" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ModelForClients.VatNumber" class="control-label"></label>
<input asp-for="ModelForClients.VatNumber" class="form-control" />
<span asp-validation-for="ModelForClients.VatNumber" class="text-danger"></span>
</div>
<div class="form-group">
<p>Utilisateur assigné: @User.Identity.Name</p>
<p>Date de création: @Model.CurrentDate </p>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-page="Index">Back to List</a>
</div>
</div>
該模型:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;
namespace ArchiProjectManager.Models
{
public class ModelForClients
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ClientId { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public string Adress { get; set; }
public string Company { get; set; }
public string LegalRepresentative { get; set; }
public string VatNumber { get; set; }
public virtual User User { get; set; }
public DateTime CreationDate { get; set; }
public DateTime ModificationDate { get; set; }
public string UserType { get; set; }
}
}
預先感謝您的幫助:D
uj5u.com熱心網友回復:
您可以添加所需的資訊作為hidden輸入。
<input name="CreationDate" value="@model.CreationDate" type="hidden" />
<input name="UserType" value="@model.UserType" type="hidden" />
uj5u.com熱心網友回復:
首先,您需要知道對于復雜型別的每個屬性,模型系結都會查看名稱模式的來源prefix.property_name。
其次,只有input和select元素可以進行表單提交。p不能通過表單提交傳遞給后端。
更改您的頁面,如下所示:
<div class="form-group">
<p>Utilisateur assigné: @User.Identity.Name</p>
//add this...
<input asp-for="ModelForClients.UserType" value="@User.Identity.Name" type="hidden" />
<input asp-for="ModelForClients.CreationDate" value="@Model.CurrentDate" type="hidden" />
<p>Date de création: @Model.CurrentDate </p>
</div>
后端:
public class CreateModel : PageModel
{
public IActionResult OnGet()
{
return Page();
}
[BindProperty]
public ModelForClients ModelForClients
{
get; set;
}
public DateTime CurrentDate = DateTime.Now;
public async Task<IActionResult> OnPostAsync()
{
//...
//no need to set the value like below:
//ModelForClients.CreationDate = CurrentDate;
//ModelForClients.UserType = currentUser
}
}
結果:

此外,@Merna Mustafa 所做的也可以,但您需要在后端設定CreationDate和UserType 屬性(而不是欄位):
[BindProperties] //use BindProperties to bind all the properties,
//then you no need add multiple BindProperty attributes
public class CreateModel: PageModel
{
public IActionResult OnGet()
{
return Page();
}
public ModelForClients ModelForClients
{
get; set;
}
//add properties here......
public DateTime CurrentDate { get; set; } = DateTime.Now;
public string UserType { get; set; }
public async Task<IActionResult> OnPostAsync()
{
ModelForClients.CreationDate = CurrentDate;
ModelForClients.UserType = UserType;
//...
return RedirectToPage("./Clients");
}
}
頁:
<input name="CreationDate" value="@Model.CurrentDate" type="hidden" />
<input name="UserType" value="@User.Identity.Name" type="hidden" />
無論如何,所有兩個解決方案都告訴您模型系結系統通過name="PropertyName".
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/353963.html
上一篇:DotnetJWTBearerTokenValidationParameters問題-未經授權?基于角色(策略)的授權不起作用
