检测页面刷新 c#

huangapple go评论71阅读模式
英文:

Detecting page refresh c#

问题

我使用ASP.NET Core Web应用程序创建了一个文档上传站点,但遇到了一个小错误,我不确定如何修复。

在我的站点上,首先创建一个'file',如下所示:

检测页面刷新 c#

然后它会出现在一个列表中,如下所示:

检测页面刷新 c#

当您点击上传附件时,它会传递上一个表格中的ID,以确保它上传到正确的文件。

检测页面刷新 c#

上传页面的代码如下,错误在于如果在选择文件之前点击上传,它会刷新页面以显示错误,然后之前传递的ID已丢失,所以myInv最终变为null。

using FarmersPortal.Data;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using File = FarmersPortal.Data.File;

namespace FarmersPortal.Pages.Admin
{
    [Authorize(Roles = "Admin")]
    public class UploadModel : PageModel
    {
        private readonly filedbContext _context;

        public UploadModel(filedbContext context)
        {

            _context = context;
        }
        public int? myID { get; set; }

        [BindProperty]
        public IFormFile file { get; set; }

        [BindProperty]
        public int? ID { get; set; }
        public void OnGet(int? id)
        {
            myID = id;
        }

        [BindProperty]
        public File File { get; set; }
        public async Task<IActionResult> OnPostAsync()
        {
            if (file != null)
            {
                if (file.Length > 0 && file.Length < 300000)
                {
                    var myInv = _context.Files.FirstOrDefault(x => x.Id == ID);
                    var date = DateTime.Today;

                    using (var target = new MemoryStream())
                    {
                        file.CopyTo(target);
                        myInv.UploadDate = date;
                        myInv.Attachment = target.ToArray();
                    }
                    if (myInv == null)
                    {
                        return NotFound();
                    }
                    else
                    {
                        File = myInv;
                    }
                    _context.Files.Update(myInv);
                    await _context.SaveChangesAsync();
                }

            }

            if (File.FileType == "Purchase Order")
            {
                return RedirectToPage("./PurchaseOrders");
            }
            else if (File.FileType == "Remittance")
            {
                return RedirectToPage("./Remittance");
            }
            else if (File.FileType == "Haulage Self Bill")
            {
                return RedirectToPage("./HaulageSelfBill");
            }
            else if (File.FileType == "Growers Return")
            {
                return RedirectToPage("./GrowersReturn");
            }
            return Page();
        }
    }
}

我不确定如何解决这个问题,有什么想法?

以上是前端代码。

英文:

I have created a document upload site with asp.net core web app's, and I have encountered a small bug but I'm not sure how to fix it.

On my site, you first create a 'file' like so:

检测页面刷新 c#

It then appears in a list like so:

检测页面刷新 c#

And when you press upload attachment, it passes the id from the previous table to ensure it uploads to the correct file.

检测页面刷新 c#

The code behind the upload page is as below, and the error is if you press upload before choosing a file, it does a page refresh to display an error and then the ID passed through before has been lost, so myInv ends up being null.

using FarmersPortal.Data;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using File = FarmersPortal.Data.File;
namespace FarmersPortal.Pages.Admin
{
[Authorize(Roles =&quot;Admin&quot;)]
public class UploadModel : PageModel
{
private readonly filedbContext _context;
public UploadModel(filedbContext context)
{
_context = context;
}
public int? myID { get; set; }
[BindProperty]
public IFormFile file { get; set; }
[BindProperty]
public int? ID { get; set; }
public void OnGet(int? id)
{
myID = id;
}
[BindProperty]
public File File { get; set; }
public async Task&lt;IActionResult&gt; OnPostAsync()
{
if (file != null)
{
if (file.Length &gt; 0 &amp;&amp; file.Length &lt; 300000)
{
var myInv = _context.Files.FirstOrDefault(x =&gt; x.Id == ID);
var date = DateTime.Today;
using (var target = new MemoryStream())
{
file.CopyTo(target);
myInv.UploadDate = date;
myInv.Attachment = target.ToArray();
}
if (myInv == null)
{
return NotFound();
}
else
{
File = myInv;
}
_context.Files.Update(myInv);
await _context.SaveChangesAsync();
}
}
if (File.FileType == &quot;Purchase Order&quot;)
{
return RedirectToPage(&quot;./PurchaseOrders&quot;);
}
else if (File.FileType == &quot;Remittance&quot;)
{
return RedirectToPage(&quot;./Remittance&quot;);
}
else if (File.FileType == &quot;Haulage Self Bill&quot;)
{
return RedirectToPage(&quot;./HaulageSelfBill&quot;);
}
else if (File.FileType == &quot;Growers Return&quot;)
{
return RedirectToPage(&quot;./GrowersReturn&quot;);
}
return Page();
}
}
}

I am not sure how to work my way around this, any ideas?

@page
@model FarmersPortal.Pages.Admin.UploadModel
@{
}
&lt;h1 style=&quot;color:white&quot;&gt;Upload File&lt;/h1&gt;
&lt;style&gt;
body {
background-image: url(&quot;http://10.48.1.215/PORTAL/hero-range-1.jpg&quot;);
height: 100%;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
&lt;/style&gt;
&lt;hr /&gt;
&lt;div class=&quot;row&quot;&gt;
&lt;div class=&quot;col-md-4&quot;&gt;
&lt;form method=&quot;post&quot; enctype=&quot;multipart/form-data&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;div class=&quot;col-md-10&quot;&gt;
&lt;p style=&quot;color:white&quot;&gt;Upload file&lt;/p&gt;
&lt;input type=&quot;hidden&quot; asp-for=&quot;@Model.ID&quot; value=&quot;@Model.myID&quot; /&gt;
&lt;input asp-for=&quot;file&quot; class=&quot;form-control&quot; accept=&quot;.pdf&quot; type=&quot;file&quot; /&gt;
&lt;span asp-validation-for=&quot;file&quot; class=&quot;text-white&quot;&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;div class=&quot;col-md-10&quot;&gt;
&lt;input class=&quot;btn btn-success&quot; type=&quot;submit&quot; value=&quot;Upload&quot; /&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;a asp-page=&quot;Index&quot;&gt;Back to List&lt;/a&gt;
&lt;/div&gt;

Above is the front end code.

答案1

得分: 1

您可以自定义错误消息

[BindProperty]
[Required(ErrorMessage = "您必须在上传此表单之前选择一个文件")]
public IFormFile file { get; set; }

您还需要将Jquery验证库添加到视图中:

@section Scripts {
@{
await Html.RenderPartialAsync("_ValidationScriptsPartial");
}
}

然后,当用户未选择任何文件并点击上传按钮时,视图将显示错误消息并停止上传表单。

英文:

You can custom the error message

[BindProperty]
[Required(ErrorMessage =&quot;You must select a file before upload this form&quot;)]
public IFormFile file { get; set; }

And you also need to add Jquery validation library to your view:

@section Scripts {
@{
await Html.RenderPartialAsync(&quot;_ValidationScriptsPartial&quot;);
}
}

Then when user don't select any file and click the upload button, View will show error message and stop uploading the form.

检测页面刷新 c#

huangapple
  • 本文由 发表于 2023年6月1日 17:37:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76380543.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定