服务器验证上的Razor页面返回而没有一些字段 [英] Razor Pages On Server Validation returning for without some fields

查看:79
本文介绍了服务器验证上的Razor页面返回而没有一些字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Razor Pages(ASP.Net Core 2.0)进行服务器端验证时遇到问题

I'm having an issue with a server side validation with Razor Pages (ASP.Net Core 2.0)

我有一个创建记录的页面

I have a page that creates a record

http://localhost:56876/Entries/Create?employeeID = 112345

我在OnGet文件后面的代码如下:

my code behind file for the OnGet looks like:

      [BindProperty]
        public EntryLog EntryLog { get; set; }
        public Employee Employee{ get; set; }
        public string empID { get; set; }

  public IActionResult OnGet(string employeeID)
        {
        empID = employeeID;
// for Selects for lookup tables
        ViewData["ReasonId"] = new SelectList(_context.Reason, "Id", "Name");
        ViewData["ReasonTypeId"] = new SelectList(_context.ReasonType, "Id", "Name");
            return Page();
        }

上面的代码可以很好地工作,并且客户端验证可以完美地进行,但是我具有业务逻辑验证,即如果输入日期在今天的日期和雇用日期之间是90,则我不应该保存该条目.

The code above works just fine and client validation works perfect but I have business logic validation that if the Entry Date is 90 between today's date and hired date that I should not let the entry to be saved.

    public async Task<IActionResult> OnPostAsync()
            {
    //Lookup current employeeID hiredDate. if is new employee do not continue.

                Employee  = await _context.Employee .FirstOrDefaultAsync(p => p.Id == EntryLog.EmployeeID);

//Server side validation
                var age = DateTime.Today.Day - Employee.HireDate.Day;
                if (age <= 90)
                {
                    ModelState.AddModelError("NewEmp", "Emp is New Warning");
                    return Page();
                }

                if (!ModelState.IsValid)
                {
                    return Page();
                }

                _context.EntryLog.Add(EntryLog);
                await _context.SaveChangesAsync();

                return RedirectToPage("./Index");
            }

我的服务器端验证有效,但是问题是当我返回Page()时; 刷新表格,两个select元素清空,而EmployeeID元素也清空.

My server side validation works but the problem is that when I return Page(); The form gets refreshed and the two selects elements get empty out and the EmployeeID element gets empty out as well.

我的猜测是OnPostAsync()上的返回Page()没有调用OnGet()方法.

My guess is that the return Page() on OnPostAsync() is not calling the OnGet() method.

如何在不丢失信息的情况下保持页面原样?

How can I keep the page as it is without loosing the info?

推荐答案

您是正确的,return Page()不调用OnGet()方法.我将尝试解释发生了什么事:

You are right the return Page() does not call OnGet() method. I´ll try to explain what is happening:

简短答案:

当您收到Post请求(OnPost())和return Page()时,服务器仅返回响应(html),它不会发出新的Get Request,因此将再次调用OnGet().

When you receive the Post request (OnPost()) and return Page() the server just returns the response (html), it does not make a new Get Request so that OnGet() get called again.

这两个select元素正在清除,因为它们是在OnGet()ViewData(是临时的)中设置的,而不是在OnPost()中设置的.我的建议是您再次在OnPost()上设置选择"(提取一种方法可以使其更简单).

The two selects elements are being cleant because they are set up in OnGet() through ViewData (that is temporary) not in OnPost(). My suggestion is that you set the "selects" again on OnPost() (extract to a method to make it easier).

详细答案:

通过键入或通过链接重定向访问页面(条目/创建)时,浏览器使用HTTP METHOD GET请求页面到服务器,这将调用OnGet方法.然后,当您发送表单(考虑method="POST")时,请求将被发送到服务器并被OnPost方法捕获.

When you access your Page (Entries/Create) by typing or being redirected through a link, the browser request the page to the server using HTTP METHOD GET, which will invoke OnGet method. Afterwards, when you send your form (considering method="POST") the request will be sent to the server and be caught by the OnPost method.

当您对return Page()进行编码时,它没有将响应发送回浏览器,说嘿,您必须再次发出GET请求(将使OnGet再次被调用)",它将返回结束响应. (html)本身.这就是为什么不调用OnGet()的原因.使OnGet()再次被调用的原因是return RedirectToPage("./Entities/Create"),但是这样做会丢失模型状态(验证结果).

When you code return Page(), it is not sending back a response to browser saying "Hey, you have to make a GET request again (what would make OnGet be called again)", it is returning the end response (html) itself. That´s why OnGet() is not being called. What would make the OnGet() be called again would be return RedirectToPage("./Entities/Create"), but doing so you would lose your Model state (validation result).

这两个select元素正在清除,因为它们是在OnGet()ViewData(是临时的)中设置的,而不是在OnPost()中设置的.我的建议是,您再次在OnPost()上设置选择"(提取一种方法可以使其更简单).

The two selects elements are being cleant because they are set up in OnGet() through ViewData (that is temporary) not in OnPost(). My suggestion is that you set the "selects" again on OnPost() (extract to a method to make it easier).

致谢.

这篇关于服务器验证上的Razor页面返回而没有一些字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆