简体   繁体   English

如何提交表单,但我的文本框将其值保留在asp.net Razor页面中

[英]How do I submit a form, but my text boxes preserve their values in asp.net Razor pages

I have a html form in which I write in a text box and submit when press a button and it saves in db but I want the page not to refresh and continue with the values ​​in the text box 我有一个html表单,我在其中写一个文本框并在按下按钮时提交,它保存在db中,但我希望页面不刷新并继续输入文本框中的值

I have see people talking about ajax but I have never worked with ajax 我见过有人在谈论Ajax,但我从未使用过Ajax

    <div class="row" style="float:left; margin: 2px ">

        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="col-md-3">
            <div class="form-group">
                <label>Pressão Injeção:</label> 
                <input id="id3" type="text" name="Pressão_de_Injeção" /> <br />
            </div>
        </div>

Here is a quick example of an AJAX call which will POST data to an controller: 这是AJAX调用的快速示例,该调用会将数据发布到控制器:

var menuId = $("ul.nav").first().attr("id");
var request = $.ajax({
  url: "Home/SaveForm",
  type: "POST",
  data: {id : menuId},
  dataType: "html"
});

request.done(function(msg) {
  console.log('success');
});

request.fail(function(jqXHR, textStatus) {
  alert( "Request failed: " + textStatus );
});

Note, that you can also pass objects to the controller by supplying them as an variable within the data object: 注意,您还可以通过将对象作为变量提供给数据对象,从而将对象传递给控制器​​:

var menuId = $("ul.nav").first().attr("id");
var obj = {
  id: menuId,
  text: "Foo"
};
var request = $.ajax({
  url: "Home/SaveForm",
  type: "POST",
  data: {name: "Jim", object: Obj},
  dataType: "html"
});

(Code adapted from an answer by apis17 .) (代码改编自apis17的答案 。)

An introduction to AJAX, can be found here: https://www.w3schools.com/js/js_ajax_intro.asp 可以在以下位置找到AJAX的简介: https : //www.w3schools.com/js/js_ajax_intro.asp

The only other method of performing what you required (without using AJAX) is to use a form submit and pass the data back to the HTML form once complete. 执行所需操作的唯一其他方法(不使用AJAX)是使用表单提交,一旦完成,便将数据传递回HTML表单。 You would then need to re-populate the fields (however, this will cause additional work on both submitting and re-populating, if (and/or when) new fields are adding if the future). 然后,您将需要重新填充字段(但是,如果将来(和/或何时)要添加新字段,这将导致提交和重新填充方面的额外工作)。

You will have to take the ajax approach. 您将不得不采用ajax方法。

But if you don't want to do that, you can always post to the controller and return the model to the view. 但是,如果您不想这样做,则始终可以将其发布到控制器并将模型返回到视图。 It will have the text you entered into the textbox.eg. 它将在文本框中输入您输入的文本。例如。

public IActionResult MyView()
        {
            return View();
        }

[HttpPost]
public IActionResult MyView(MyModel model)
            {
                return View(model);
            }

Hope this helps as an alternative. 希望这可以作为替代选择。

暂无
暂无

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

相关问题 如何在ASP.NET Razor页面中为我的CRUD操作使用多个基类 - How do I use multiple base classes for my CRUD operation in ASP.NET Razor pages 如何在ASP.NET MVC中修改Razor View Engine生成的默认表单值? - How do I modify default form values generated by Razor View Engine in ASP.NET MVC? 如何在ASP.NET MVC中将表单提交到数据库中 - How do I submit a form into a database in asp.net MVC 如何使用 ASP.NET Razor 页面从 cshtml 中的 ParseObject 呈现自定义字段值? - How can I render custom field values from a ParseObject in cshtml using ASP.NET Razor Pages? 如何使用提交按钮发布 SQL 数据 asp.net c# razor - How do I post SQL data using submit button in asp.net c# razor 如何从ASP.Net网页和剃须刀上使用MongoDB? - How do I work with MongoDB from ASP.Net web pages and razor? 如何在ASP.Net Core 2.0 Razor页面中发布IFormFile? - How do I post an IFormFile in ASP.Net Core 2.0 Razor Pages? 如何在尝试使用 Razor Pages 删除 ASP.NET Core 中的记录时显示确认消息 - How do I display a confirmation message with trying to delete a record in ASP.NET Core using Razor Pages 如何在自定义构建的 ASP.NET Core Razor Pages 可编辑网格中发布行 ID? - How do I post the row id in a custom built ASP.NET Core Razor Pages editable Grid? 如何将 Razor Pages ASP.NET 应用程序连接到现有的非本地 MSSQL 数据库? - How do I connect a Razor Pages ASP.NET app to an already existing, not local MSSQL database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM