简体   繁体   English

使用Ajax ASP.NET发布

[英]Post with ajax ASP.NET

Controller 调节器

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Index(Contact contact)
    {
        using (LawContext DB = new LawContext())
        {
            if (ModelState.IsValid)
            {
                DB.Contact.Add(contact);
                DB.SaveChanges();
                return Json("OK");
            }

            return View();
        }
    }

View 视图

                @using (Html.BeginForm("Index", "Contact", FormMethod.Post))
                {
                        @if (!ViewData.ModelState.IsValid)
                        {
                                @Html.ValidationSummary("", new { @class = "alert-danger" })
                        }
                        @Html.AntiForgeryToken()
                            @Html.TextBoxFor(m => m.Lastname, new { @class = "form-control margin-bottom10", @placeholder = "Name" })

                            @Html.TextBoxFor(m => m.Mail, new { @class = "form-control margin-bottom10", @placeholder = "Mail" })

                            @Html.TextBoxFor(m => m.Phone, new { @class = "form-control margin-bottom10", @placeholder = "Phone" })

                            @Html.TextAreaFor(m => m.Message, new { @class = "form-control margin-bottom10", @placeholder = "Message", @rows = "3" })

                            <input type="submit" value="Submit" class="btn btn-darkgray" id="btn_contact" />
                }

JavaScript JavaScript的

$(document).ready(function () {
    $("#btn_contact").click(function () {

        $.ajax(
            {
                type: "POST",
                url: "Contact/Index", 
                data: { 
                    Lastname: $("#Lastname").val(),
                    Mail: $("#Mail").val(),
                    Phone: $("#Phone").val(),
                    Message: $("#Message").val()
                },
                dataType: "json",
                async: true,
                processData: false,
                cache: false,
                contentType: "application/json; charsetset=utf8",
                success: function (data) {
                    alert('success');
                },
                failure: function (data) {
                    alert('failure');
                },
                error: function (data) {
                    alert('error');
                }
            });
    });
});

I do not understand why this is happening Do not normally have to be alerted? 我不明白为什么会这样?通常不需要提醒吗? How can I do this properly? 如何正确执行此操作? I would like your help. 需要您的帮助。 I have studied many examples. 我研究了很多例子。 I guess I lost hope I could do without asking. 我想我没有希望就可以做到的希望了。

I would like to work well. 我想工作顺利。

Try changing your submit button to: 尝试将您的提交按钮更改为:
<input type="button" value="Submit" class="btn btn-darkgray" id="btn_contact" />

Or you can leave it as is and in your Ajax call, do: 或者,您可以保留它,并在Ajax调用中执行以下操作:

$(document).ready(function () {
    $("#btn_contact").click(function (event) {
        event.preventDefault();

        $.ajax(
            {
                type: "POST",
                url: "Contact/Index", 
                data: { 
                    Lastname: $("#Lastname").val(),
                    Mail: $("#Mail").val(),
                    Phone: $("#Phone").val(),
                    Message: $("#Message").val()
                },
                dataType: "json",
                async: true,
                processData: false,
                cache: false,
                contentType: "application/json; charsetset=utf8",
                success: function (data) {
                    alert('success');
                },
                failure: function (data) {
                    alert('failure');
                },
                error: function (data) {
                    alert('error');
                }
            });
    });
});

This is so as to prevent the default behaviour of the submit type button which sends the form to the action url ( <form action="contact/index" method="POST" ... ) which in your case you don't have. 这是为了防止提交类型按钮的默认行为,该按钮会将表单发送到操作网址( <form action="contact/index" method="POST" ... ),在您的情况下,您没有。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM