简体   繁体   English

提交后保留表单值 - Razor 页

[英]Keep form values after submit - Razor Pages

I'm struggling with form clear after submit search boxes in my .net Core - Razor Pages web app.在我的 .net 核心 - Razor 页面 web 应用程序中提交搜索框后,我正在努力清除表单。 I need to persist searched values after onPostAction which are called from form submit button.我需要在从表单提交按钮调用的 onPostAction 之后保留搜索值。

Here is my index.cshtml (simplified to one searchbox and 2 buttons - post & clear filter)这是我的 index.cshtml(简化为一个搜索框和 2 个按钮 - 发布和清除过滤器)

    <form method="post">
      <div class="form-group form-inline row ">
        <label class="col-md-2 font-weight-bold control-label">Phone Number</label>
        <input type="search" name="searchPhoneNumber" value="@ViewData["sPhoneNumber"]?.ToString()"/>
      </div>
      <div class="form-group form-inline row ">
        <input type="submit" value="Search"/>
        <button type="reset" onclick="$this.form.reset()">Clear All</button>
    </div>

Then in my index.cshtml.cs I have OnPostAsync task (again simplified)然后在我的 index.cshtml.cs 中我有 OnPostAsync 任务(再次简化)

[ViewData]
public string sPhoneNumber{ get; set; }
public async Task OnPostAsync(string searchPhoneNumber)
{
if (!String.IsNullOrEmpty(searchAlcatel))
        {
            records = records.Where(s => s.PhoneNumber.Contains(searchPhoneNumber));
            ViewData["sPhoneNumber"] = searchPhoneNumber;
        }
        else { ViewData["sPhoneNumber"] = ""; }
}
Model = await records.AsNoTracking().OrderBy(item => item.Surename).ToListAsync();

Now the problem.现在的问题。 This way form keeps data values from client after submit button fires OnPost method, which is what I need.这种方式在提交按钮触发 OnPost 方法后,表单会保留来自客户端的数据值,这正是我所需要的。 But,., also this solution breaks functionality of "reset" button, because it cannot clear value from that search input, Moreover.但是,这个解决方案也破坏了“重置”按钮的功能,因为它无法从该搜索输入中清除值,而且。 If I manualy clear value of that search box and hit the "reset" button, it spawns lastly used value back in the field, which is definitelly not what I want.如果我手动清除该搜索框的值并点击“重置”按钮,它会在该字段中生成最后使用的值,这绝对不是我想要的。

Is there any way how to keep form values after submit and also make "reset" button working on form data values in same time please?有什么办法可以在提交后保留表单值,同时让“重置”按钮对表单数据值起作用吗?

Thank you very much in advance for any kind of help.非常感谢您提供任何帮助。

I solve this as @Aluan Haddad suggested.我按照@Aluan Haddad 的建议解决了这个问题。 To reset values I had to make custom function in javascript:要重置值,我必须在 javascript 中自定义 function:

<script type="text/javascript">
            function resetForm() {
                document.getElementById("searchPhone").value = "";
                document.getElementById("searchFax").value = "";
            }
        </script>

and call it from onClic action like (onclick="resetForm()")并从 onClic 操作中调用它,例如 (onclick="resetForm()")

Now it works fine for me... Thank you very much for help !现在它对我来说很好......非常感谢你的帮助!

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

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