Asp.net 创建新条目时填充表单中最后一行的数据
我有一个表单可以为注释创建新的数据条目。创建全新的条目效果很好。但是,当我已经为我的实体创建了一个条目时,我希望填充表单中最后一个条目的数据 我已尝试修改OnGet操作,以包含来自上一个条目的数据。我将OnGet代码从Edit视图复制到Create视图中。但是,如果我这样做,创建页面将不再显示 我有以下型号:Asp.net 创建新条目时填充表单中最后一行的数据,asp.net,razor-pages,actionresult,Asp.net,Razor Pages,Actionresult,我有一个表单可以为注释创建新的数据条目。创建全新的条目效果很好。但是,当我已经为我的实体创建了一个条目时,我希望填充表单中最后一个条目的数据 我已尝试修改OnGet操作,以包含来自上一个条目的数据。我将OnGet代码从Edit视图复制到Create视图中。但是,如果我这样做,创建页面将不再显示 我有以下型号: public class ProjectComment { public int Id { get; set; } public int?
public class ProjectComment
{
public int Id { get; set; }
public int? ProjectId { get; set; }
public Project Project { get; set; }
public int RAGStatusId { get; set; }
public RAGStatus RAGStatus { get; set; }
public string StatusComment { get; set; }
public string EscalationComment { get; set; }
public string GeneralComment { get; set; }
public double? EOQ { get; set; }
public DateTime LastUpdateDate { get; set; }
public ProjectComment ()
{
this.LastUpdateDate = DateTime.UtcNow;
}
创建表单create.cshtml:
@page
@model SimpleProjectReporting.Pages.ClientDetails.CreateModel
@{
ViewData["Title"] = "Create";
}
<h1>Create</h1>
<h4>ProjectComment</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="ProjectComment.ProjectId" class="control-label"></label>
<select asp-for="ProjectComment.ProjectId" class="form-control" asp-items="ViewBag.ProjectId"><option value="" default="" selected="">-- Select --</option></select>
</div>
<div class="form-group">
<label asp-for="ProjectComment.RAGStatusId" class="control-label"></label>
<select asp-for="ProjectComment.RAGStatusId" class="form-control" asp-items="ViewBag.RAGStatusId"><option value="" default="" selected="">-- Select --</option></select>
</div>
<div class="form-group">
<label asp-for="ProjectComment.StatusComment" class="control-label"></label>
<input asp-for="ProjectComment.StatusComment" class="form-control" />
<span asp-validation-for="ProjectComment.StatusComment" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ProjectComment.EOQ" class="control-label"></label>
<input asp-for="ProjectComment.EOQ" class="form-control" />
<span asp-validation-for="ProjectComment.EOQ" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
原始Create.cshtml.cs操作:
[BindProperty]
public ProjectComment ProjectComment { get; set; }
public IActionResult OnGet()
{
ViewData["ProjectId"] = new SelectList(_context.Project.Where(a => a.IsArchived == false), "Id", "ProjectName");
ViewData["RAGStatusId"] = new SelectList(_context.RAGStatus.Where(a => a.IsActive == true), "Id", "RAGStatusName");
return Page();
}
// To protect from overposting attacks, please 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.ProjectComment.Add(ProjectComment);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
修改后的Create.cshtml.cs OnGet操作:
public async Task<IActionResult> OnGetAsync(int? id)
{
if (id == null)
{
return NotFound();
}
ProjectComment = await _context.ProjectComment
.Include(p => p.Project)
.Include(p => p.RAGStatus).FirstOrDefaultAsync(m => m.Id == id);
if (ProjectComment == null)
{
return NotFound();
}
当以我的方式修改操作时,页面不再显示404错误。
我想用数据库中最后一个条目的数据填充create表单。如果没有注释,创建页面将只填充项目名称。我猜您没有将id参数发送到post操作 因此,请尝试在表单标签下添加这一行:
您正在尝试访问ProjectComment表的最后一条记录。 有多种方法可以查找数据表的最后一条记录。但让我们保持简单 您有一个基于整数的标识列,它是自动递增的。因此,您可以简单地使用以下方法来获取表中最后创建的数据 在您的OnGetAsync方法中:
现在,由于您已经在数据库中找到了最后记录的数据,因此可以使用该对象。首先,感谢Burak提供了上述解决方案,当您希望显示表中的最后一行时,该解决方案可以工作。通过使用相同的方法并根据记录的Id查找记录,这帮助我解决了问题。 我已将Create.cshtml.cs文件中的代码修改如下:
public async Task<IActionResult> OnGetAsync(int? id, int projectid)
{
//This will find the "ProjectComment" object.
var projectComment = _context.ProjectComment.Find(id);
//This will display the 'ProjectComment' on the page
ProjectComment = projectComment;
if (id == null)
{
ProjectComment = projectComment;
ViewData["ProjectId"] = new SelectList(_context.Project, "Id", "ProjectName", projectid);
return Page();
}
ViewData["ProjectId"] = new SelectList(_context.Project, "Id", "ProjectName");
return Page();
}
当还没有创建注释时,我使用int projectid填充项目的下拉菜单。谢谢。这使我更进一步。但是,我现在收到错误“当identity_insert设置为OFF时,无法在表“ProjectComment”中为identity列插入显式值”。这是因为Id是自动生成且唯一的。我怎样才能避开这个问题呢?那么您想将数据插入主键列吗?我只想填充其他字段。主键列应该是自动生成的。如果您试图获取数据库中最后一条记录的值,则应该更改方法。尝试使用以下命令查找最后一条记录:int max=db.ProjectComment.Maxp=>p.Id;对不起,我对编码很陌生。在Create.cshtml或Create.cshtml.cs中搜索哪个文件?如何显示Create.cshtml中最后一条注释的内容?你能提供一个例子吗?你好,Burak,我终于有时间测试你的解决方案了。拿到最后一张唱片效果很好。但是,我需要添加注释的项目的最后一条记录。因此,我还需要一个附加约束:显示注释的projectd的最后一个ProjectComment。当我从索引页面导航时,如何添加该约束?该页面列出了我对按客户端分组的所有项目的所有注释?
//maxId will be the maximum value of "Id" columns. Which means that the maximum value is the last recorded value.
int maxId = _context.ProjectComment.Max(i => i.Id);
//And this line will bring you the last recorded "ProjectComment" object.
var projectComment = _context.ProjectComment.Find(maxId);
//You can assign it to your above 'ProjectComment' property if you want to..
ProjectComment = projectComment
public async Task<IActionResult> OnGetAsync(int? id, int projectid)
{
//This will find the "ProjectComment" object.
var projectComment = _context.ProjectComment.Find(id);
//This will display the 'ProjectComment' on the page
ProjectComment = projectComment;
if (id == null)
{
ProjectComment = projectComment;
ViewData["ProjectId"] = new SelectList(_context.Project, "Id", "ProjectName", projectid);
return Page();
}
ViewData["ProjectId"] = new SelectList(_context.Project, "Id", "ProjectName");
return Page();
}