在 .Net Core 3 中使用 Ajax 提交表单而不刷新页面

huangapple go评论97阅读模式
英文:

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:

&lt;div class=&quot;form-popup&quot; id=&quot;myForm&quot;&gt;
&lt;div id=&quot;CRDone&quot; style=&quot;display:none;&quot;&gt;
&lt;i class=&quot;fa fa-check&quot; style=&quot;color:green;&quot; aria-hidden=&quot;true&quot;&gt;&lt;/i&gt;
&lt;br/&gt;
&lt;p style=&quot;color:green;&quot;&gt;We&#39;ve recieved your request&lt;/p&gt;
&lt;/div&gt;
&lt;form id=&quot;CRForm&quot; method=&quot;post&quot; asp-action=&quot;SaveContactRequest&quot; class=&quot;form-container&quot;&gt;
&lt;h1&gt;Get in touch&lt;/h1&gt;
&lt;label for=&quot;CRName&quot;&gt;&lt;b&gt;Name&lt;/b&gt;&lt;/label&gt;
&lt;input id=&quot;CRName&quot; type=&quot;text&quot; placeholder=&quot;What&#39;s your name&quot; name=&quot;Name&quot; required&gt;
&lt;label for=&quot;CRCompany&quot;&gt;&lt;b&gt;Company&lt;/b&gt;&lt;/label&gt;
&lt;input id=&quot;CRCompany&quot; type=&quot;text&quot; placeholder=&quot;Where do you work&quot; name=&quot;Company&quot; required&gt;
&lt;label for=&quot;CRDepartment&quot;&gt;&lt;b&gt;Department&lt;/b&gt;&lt;/label&gt;
&lt;input id=&quot;CRDepartment&quot; type=&quot;text&quot; placeholder=&quot;Enter your Department&quot; name=&quot;Department&quot; required&gt;
&lt;label for=&quot;CRPosition&quot;&gt;&lt;b&gt;Position&lt;/b&gt;&lt;/label&gt;
&lt;input id=&quot;CRPosition&quot; type=&quot;text&quot; placeholder=&quot;Enter your Position&quot; name=&quot;Position&quot; required&gt;
&lt;label for=&quot;CRPhone&quot;&gt;&lt;b&gt;Phone Number&lt;/b&gt;&lt;/label&gt;
&lt;input id=&quot;CRPhone&quot; type=&quot;text&quot; placeholder=&quot;Write your Mobile Number&quot; name=&quot;Phone&quot; required&gt;
&lt;label for=&quot;CREmail&quot;&gt;&lt;b&gt;Email&lt;/b&gt;&lt;/label&gt;
&lt;input id=&quot;CREmail&quot; type=&quot;text&quot; placeholder=&quot;Write your Email&quot; name=&quot;Email&quot; required&gt;
&lt;label for=&quot;CRMessage&quot;&gt;&lt;b&gt;Message&lt;/b&gt;&lt;/label&gt;
&lt;input id=&quot;CRMessage&quot; type=&quot;text&quot; placeholder=&quot;Write your Message&quot; name=&quot;Message&quot;&gt;
&lt;button type=&quot;submit&quot; class=&quot;btn&quot;&gt;Request&lt;/button&gt;
&lt;/form&gt;
&lt;/div&gt;

Javascript

    var $this = $(this);
var frmValues = $this.serialize();
$.ajax({
type: $this.attr(&#39;method&#39;),
url: $this.attr(&#39;action&#39;),
data: frmValues,
processData: false,
contentType: false
}).done(function (result) {
console.log(result);
if (result == true) {
$(&quot;CRDone&quot;).show();
$(&quot;CRForm&quot;).hide();
}
else {
alert(&#39;an Error has occurred, please try again!&#39;);
}
}).fail(function (result) {
alert(&#39;an Error has occurred, please try again&#39;);
});
});

Controller

[HttpPost]
public async Task&lt;bool&gt; 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

&lt;form id=&quot;CRForm&quot; method=&quot;post&quot; onsubmit=&quot;return false;&quot;

or you could use jquery try:

 $(document).on(&#39;submit&#39;, &#39;#CRForm&#39;, function(e){
e.preventDefault();
return false;
});

it could also help to just not use a button type="submit"
maybe just use a span

&lt;span class=&quot;btn&quot;&gt;Request&lt;/span&gt;

update according to your code:

$(&quot;#CRForm&quot;).on(&quot;submit&quot;, function (event) {
event.preventDefault();
var $this = $(this);
...

huangapple
  • 本文由 发表于 2020年1月3日 17:36:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576126.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定