[英]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.