简体   繁体   English

属性值在 Razor 页面中重置 OnPost

[英]Property values reset OnPost in Razor Pages

I am trying to keep the values of my property for my OnPost method.我试图为我的 OnPost 方法保留我的属性值。 I set the values in the OnGet method, when I change from the last page to the current.当我从最后一页更改为当前页面时,我在 OnGet 方法中设置了值。 Here's the HTML for the button that redirects to the page with my property:这是使用我的属性重定向到页面的按钮的 HTML:

<a class="btn" type="button" asp-page="CreateDate" asp-route-dateString="@Model.model.StartDate.AddDays(i).ToString("yyyy-MM-dd")"><i class="fa fa-plus-square"></i></a>

Here's the OnGet method:这是 OnGet 方法:

        [BindProperty]
    public DateTime eventDate { get; set; }

    public void OnGet(string dateString)
    {
        var stringToDate = DateTime.Parse(dateString);
        eventDate = stringToDate;
    }

Here's the OnPost:这是 OnPost:

        public IActionResult OnPost()
    {
        Date date = new Date();
        date.Date1 = eventDate;
        date.Title = Request.Form["Title"];
        date.Description = Request.Form["Description"];

        dateService.AddDate(date, userID);

        return RedirectToPage("Calendar");
    }

And lastly the HTML for the OnPost method:最后是 OnPost 方法的 HTML:

<div>
<h5 class="pageTitle">Udfyld formular for at oprette et event for denne dato: @Model.eventDate.ToString("D")</h5>
<form method="post">
    Navn på event:
    <br />
    <input name="Title" type="text" class="form-control input-lg" required autofocus />
    <br />
    Beskrivelse:
    <br />
    <input name="Description" type="text" class="form-control input-lg" required autofocus />
    <br />
    <input type="submit" value="Opret Event" class="btn btn-primary">
</form>

I am still very new, so I'm just (hopefully) looking for a simple answer.我还是很新,所以我只是(希望)寻找一个简单的答案。 Thanks!谢谢!

As you redirect to Calendar page in your OnPost method,you will go to the Calendar page,so you can pass property to the Calendar page and keep property in Calendar PageModel.当您在 OnPost 方法中重定向到日历页面时,您将 go 到日历页面,因此您可以将属性传递给日历页面并将属性保留在日历页面模型中。

OnPost method: OnPost 方法:

public IActionResult OnPost()
    {
        Date date = new Date();
        date.Date1 = eventDate;
        date.Title = Request.Form["Title"];
        date.Description = Request.Form["Description"];

        dateService.AddDate(date, userID);

        return RedirectToPage("Calendar",date);
    }

Calendar.cshtml.cs:日历.cshtml.cs:

public class CalendarModel : PageModel
    {
        [BindProperty]
        public Date date { get; set; }
        public void OnGet(Date Date)
        {
            date = Date;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM