如何防止在按下提交按钮时重置获取到的数值?

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

How to prevent values on get from reset when press submit button?

问题

I work on razor page asp.net . I face issue my issue values on get reset after click submit button so when click submit button and check values on POST I found it become null

so How to avoid this behavior OR with another meaning how to retain values on get without reset when click submit button

from my debug details as below

1 - when page load i check user id it have value 9595

public void OnGet()
{
string MyUserData = Request.Cookies["myUserData"];
UserAuthResponse? userAuthResponse = System.Text.Json.JsonSerializer.Deserialize(MyUserData);
string UserID = userAuthResponse.Data.UserID;
}

2 - Now I will click submit button so break point hit on post method

public async Task OnPost(UC.ADC.Core.Entities.SQL.Branch branch)
{
//userAuthResponse.Data.UserID become null
}
when check values of userAuthResponse.Data.UserID it become null

so how to preserve or retain value of userAuthResponse.Data.UserID after make get to use it on post method

form view as below :

    <div class="form-group row" style="margin-left:200px;font-size:15px;font-family: 'Open Sans' , sans-serif;font-weight: bold;">
        <button type="submit" class="col-sm-1 btn btn-primary">Submit</button>
        
    </div>
</form>

issue debug details as below :

如何防止在按下提交按钮时重置获取到的数值?

英文:

I work on razor page asp.net . I face issue my issue values on get reset after click submit button so when click submit button and check values on POST I found it become null

so How to avoid this behavior OR with another meaning how to retain values on get without reset when click submit button

from my debug details as below

1 - when page load i check user id it have value 9595

   public void OnGet()
    {
        string MyUserData = Request.Cookies[&quot;myUserData&quot;];
        UserAuthResponse? userAuthResponse = System.Text.Json.JsonSerializer.Deserialize&lt;UserAuthResponse&gt;(MyUserData);
        string UserID = userAuthResponse.Data.UserID;
    }

2 - Now I will click submit button so break point hit on post method

public async Task OnPost(UC.ADC.Core.Entities.SQL.Branch branch)
{
//userAuthResponse.Data.UserID become null
}

when check values of userAuthResponse.Data.UserID it become null

so how to preserve or retain value of userAuthResponse.Data.UserID after make get
to use it on post method

form view as below :

&lt;form method=&quot;post&quot;&gt;
       
        &lt;div class=&quot;form-group row&quot; style=&quot;margin-left:200px;font-size:15px;font-family: &#39;Open Sans&#39; , sans-serif;font-weight: bold;&quot;&gt;
            &lt;button type=&quot;submit&quot; class=&quot;col-sm-1 btn btn-primary&quot;&gt;Submit&lt;/button&gt;
            
        &lt;/div&gt;
    &lt;/form&gt;

issue debug details as below :

如何防止在按下提交按钮时重置获取到的数值?

答案1

得分: 1

在你的 PageModel 类中添加一个公共属性,用来表示这个值,并在其上应用 BindProperty 属性。然后,在你的表单中添加一个包含要在请求之间保持的值的隐藏字段:

[BindProperty]
public string UserID { get; set; }

public void OnGet()
{
    string MyUserData = Request.Cookies["myUserData"];
    UserAuthResponse? userAuthResponse = System.Text.Json.JsonSerializer.Deserialize<UserAuthResponse>(MyUserData);
    UserID = userAuthResponse.Data.UserID;
}

然后,在表单中:

<form method="post">
    <input type="hidden" asp-for="UserID" />
    <div class="form-group row" style="margin-left:200px;font-size:15px;font-family: 'Open Sans', sans-serif;font-weight: bold;">
        <button type="submit" class="col-sm-1 btn btn-primary">Submit</button>
    </div>
</form>

提交的值应该会自动绑定到这个公共属性。

这是在 HTTP 请求之间保持值的众多状态管理策略之一:https://www.learnrazorpages.com/razor-pages/state-management

英文:

Add a public property to your PageModel class that represents the value and apply a BindProperty attribute to it. Then add a hidden field to your form that contains the value you want to persist across requests:

[BindProperty]
public string UserID {get;set;}
public void OnGet()
{
    string MyUserData = Request.Cookies[&quot;myUserData&quot;];
    UserAuthResponse? userAuthResponse = System.Text.Json.JsonSerializer.Deserialize&lt;UserAuthResponse&gt;(MyUserData);
    UserID = userAuthResponse.Data.UserID;
}

Then the form:

&lt;form method=&quot;post&quot;&gt;
    &lt;input type=&quot;hidden&quot; asp-for=&quot;UserID&quot; /&gt;
    &lt;div class=&quot;form-group row&quot; style=&quot;margin-left:200px;font-size:15px;font-family: &#39;Open Sans&#39; , sans-serif;font-weight: bold;&quot;&gt;
        &lt;button type=&quot;submit&quot; class=&quot;col-sm-1 btn btn-primary&quot;&gt;Submit&lt;/button&gt;
    &lt;/div&gt;
&lt;/form&gt;

The posted value should automatically get bound to the public property.

This is one of many state management strategies for persisting values across HTTP requests: https://www.learnrazorpages.com/razor-pages/state-management

huangapple
  • 本文由 发表于 2023年6月8日 07:30:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427693.html
匿名

发表评论

匿名网友

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

确定