在创建新条目时从表格的最后一行填充数据

问题描述 投票:0回答:2

我有一个表单可以创建新的数据条目以供注释。创建全新的条目可以正常工作。但是,当我已经为我的实体创建了一个条目时,我想从表单中的最后一个条目中填充数据。

我试图修改OnGet操作以包括最后一个条目中的数据。我将OnGet代码从“编辑”视图复制到了“创建”视图中。但是,如果执行此操作,将不再显示“创建”页面。

我有以下模型:

    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错误)。我想用数据库中最后一个条目中的数据填充创建表单。如果没有评论,则创建页面将仅填充项目的名称。

asp.net razor-pages actionresult
2个回答
0
投票

我猜您没有将“ id”参数发送到您的帖子操作中。

所以您可以尝试在form标签下添加此行:

<form method="post">
<input type="hidden" id="ProjectComment.Id" name="id" value="ProjectComment.Id" />

0
投票

您正在尝试到达ProjectComment表的最后一条记录。有多种方法可以查找数据表的最后一条记录。但请保持简单。

您有一个基于整数的标识列,即Auto Increment。因此,您可以简单地使用以下方法来获取表的最后创建的数据。

在您的OnGetAsync()方法中:

//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 

现在,由于您已找到数据库中最后记录的数据,因此可以使用该对象。

© www.soinside.com 2019 - 2024. All rights reserved.