简体   繁体   中英

How to store data in a hidden field and the pass it between pages in razor pages

So I am trying to select a value from dropdown list and passing it as query string in a another view this works fine on get but for post I want to store it in a hidden field

<div style="margin: 30px">
     <label asp-for="DropDown" class="control-label">DropDownList</label>
     <select asp-items="Model.dropdown" 
             asp-for="targetvalue" 
             value ="targetvalue" 
             type="hidden" class="form-control">
     </select>

Not sure if this is correct how do I retrieve the hidden field to use it in post

恕我直言,您应该将该值绑定到页面模型字段,然后在表单中生成一个隐藏的输入;

<input type="hidden" asp-for="targetvalue" />

You can use any of below methods, But the recommended way is "Model Binding".

  1. You can access the value by passing the key as an indexer to the Request.Form collection, like below:

     var number = Request.Form["targetvalue"];
  2. Add a suitable property to the PageModel and to allow model binding to apply the posted value to the property: like below:

     public IndexModel : PageModel { [BindProperty] public int targetvalue { get; set; } public void OnPost() { // posted value is assigned to the targetvalue property automatically } }

For this example the Drop-down list can be rendered in different ways, But once it rendered into HTML it would look like below:

<select name="targetvalue">
    <option value="">Select a number</option>
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
</select>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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