英文:
Submit Form in .Net Core 3 using Ajax without page refresh
问题
HTML:
<div class="form-popup" id="myForm">
<div id="CRDone" style="display:none;">
<i class="fa fa-check" style="color:green;" aria-hidden="true"></i>
<br/>
<p style="color:green;">我们已收到您的请求</p>
</div>
<form id="CRForm" method="post" asp-action="SaveContactRequest" class="form-container">
<h1>联系我们</h1>
<label for="CRName"><b>姓名</b></label>
<input id="CRName" type="text" placeholder="请输入您的姓名" name="Name" required>
<label for="CRCompany"><b>公司</b></label>
<input id="CRCompany" type="text" placeholder="您所在的公司" name="Company" required>
<label for="CRDepartment"><b>部门</b></label>
<input id="CRDepartment" type="text" placeholder="请输入您的部门" name="Department" required>
<label for="CRPosition"><b>职位</b></label>
<input id="CRPosition" type="text" placeholder="请输入您的职位" name="Position" required>
<label for="CRPhone"><b>电话号码</b></label>
<input id="CRPhone" type="text" placeholder="请输入您的手机号码" name="Phone" required>
<label for="CREmail"><b>电子邮件</b></label>
<input id="CREmail" type="text" placeholder="请输入您的电子邮件" name="Email" required>
<label for="CRMessage"><b>留言</b></label>
<input id="CRMessage" type="text" placeholder="请输入您的留言" name="Message">
<button type="submit" class="btn">请求</button>
</form>
</div>
Javascript:
$("#CRForm").on("submit", function (event) {
var $this = $(this);
var frmValues = $this.serialize();
$.ajax({
type: $this.attr('method'),
url: $this.attr('action'),
data: frmValues,
processData: false,
contentType: false
}).done(function (result) {
console.log(result);
if (result == true) {
$("#CRDone").show();
$("#CRForm").hide();
} else {
alert('发生错误,请重试!');
}
}).fail(function (result) {
alert('发生错误,请重试!');
});
});
Controller:
[HttpPost]
public async Task<bool> SaveContactRequest(ContactRequest request)
{
return await request.SaveRequest();
}
我也尝试过将函数设置为按钮的onclick事件,而不是表单提交,并尝试从按钮事件调用该函数,但所有方法都会导致页面刷新并在空白白页上显示"true"或"false"。
英文:
I Want to submit my form in .Net Core 3 MVC without refreshing the page, but it still refreshes.
HTML:
<div class="form-popup" id="myForm">
<div id="CRDone" style="display:none;">
<i class="fa fa-check" style="color:green;" aria-hidden="true"></i>
<br/>
<p style="color:green;">We've recieved your request</p>
</div>
<form id="CRForm" method="post" asp-action="SaveContactRequest" class="form-container">
<h1>Get in touch</h1>
<label for="CRName"><b>Name</b></label>
<input id="CRName" type="text" placeholder="What's your name" name="Name" required>
<label for="CRCompany"><b>Company</b></label>
<input id="CRCompany" type="text" placeholder="Where do you work" name="Company" required>
<label for="CRDepartment"><b>Department</b></label>
<input id="CRDepartment" type="text" placeholder="Enter your Department" name="Department" required>
<label for="CRPosition"><b>Position</b></label>
<input id="CRPosition" type="text" placeholder="Enter your Position" name="Position" required>
<label for="CRPhone"><b>Phone Number</b></label>
<input id="CRPhone" type="text" placeholder="Write your Mobile Number" name="Phone" required>
<label for="CREmail"><b>Email</b></label>
<input id="CREmail" type="text" placeholder="Write your Email" name="Email" required>
<label for="CRMessage"><b>Message</b></label>
<input id="CRMessage" type="text" placeholder="Write your Message" name="Message">
<button type="submit" class="btn">Request</button>
</form>
</div>
Javascript
var $this = $(this);
var frmValues = $this.serialize();
$.ajax({
type: $this.attr('method'),
url: $this.attr('action'),
data: frmValues,
processData: false,
contentType: false
}).done(function (result) {
console.log(result);
if (result == true) {
$("CRDone").show();
$("CRForm").hide();
}
else {
alert('an Error has occurred, please try again!');
}
}).fail(function (result) {
alert('an Error has occurred, please try again');
});
});
Controller
[HttpPost]
public async Task<bool> SaveContactRequest(ContactRequest request)
{
return await request.SaveRequest();
}
I've also tried to make the function onclick for the button instead of form submit and tried to call the function from button events, all tends to change the page and shows true or false word in a blank white page.
答案1
得分: 4
你需要阻止提交表单 "non-ajax",可以简单地这样做:
<form id="CRForm" method="post" onsubmit="return false;">
或者你可以尝试使用 jQuery:
$(document).on('submit', '#CRForm', function(e){
e.preventDefault();
return false;
});
还可以考虑不使用按钮的 type="submit"
,而是使用 <span>
:
<span class="btn">Request</span>
根据你的代码进行更新:
$("#CRForm").on("submit", function (event) {
event.preventDefault();
var $this = $(this);
...
});
英文:
you need to prevent submitting the form "non-ajax"
this could be as simple as
<form id="CRForm" method="post" onsubmit="return false;"
or you could use jquery try:
$(document).on('submit', '#CRForm', function(e){
e.preventDefault();
return false;
});
it could also help to just not use a button type="submit"
maybe just use a span
<span class="btn">Request</span>
update according to your code:
$("#CRForm").on("submit", function (event) {
event.preventDefault();
var $this = $(this);
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论