【问题标题】:Submit Form in .Net Core 3 using Ajax without page refresh使用Ajax在.Net Core 3中提交表单而不刷新页面
【发布时间】:2020-04-21 20:57:30
【问题描述】:

我想在 .Net Core 3 MVC 中提交我的表单而不刷新页面,但它仍然会刷新。

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');
        });
          });

控制器

[HttpPost]
        public async Task<bool> SaveContactRequest(ContactRequest request)
        {
            return await request.SaveRequest();
        }

我也尝试过为按钮设置onclick函数而不是表单提交,并尝试从按钮事件中调用该函数,所有这些都倾向于更改页面并在空白页面中显示真词或假词。

【问题讨论】:

  • 您可以使用普通按钮而不是提交类型并在点击事件中调用JavaScript函数

标签: javascript jquery .net asp.net-core asp.net-core-mvc


【解决方案1】:

你需要防止提交表单“non-ajax”

这可以像

一样简单
<form id="CRForm" method="post" onsubmit="return false;"

或者你可以使用 jquery 试试:

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

不使用按钮 type="submit"
也有帮助 也许只是使用一个跨度

<span class="btn">Request</span>

根据你的代码更新:

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

【讨论】:

  • 您可以将其保留为按钮。只需更改类型:&lt;button type="button" class="btn"&gt;Request&lt;/button&gt;
  • 第二种和第三种解决方案。不工作,页面仍然刷新并返回 true 或 false。
  • 第一个解决方案完全阻止表单提交
猜你喜欢
  • 1970-01-01
  • 2017-03-17
  • 1970-01-01
  • 2020-10-13
  • 2017-04-09
  • 1970-01-01
  • 2013-07-10
  • 2014-03-03
  • 1970-01-01
相关资源
最近更新 更多