为什么Ajax.BeginForm无法正常工作

时间:2017-02-08 16:57:37

标签: javascript jquery ajax confirm ajax.beginform

我试图在我的网站上添加一个表单,所以当用户单击该按钮时,它应该在那里作为loadingElementId传递一些消息。确认不起作用,我不知道为什么。我使用的是Chrome浏览器。

以下是代码:

@model HaveYouSeenMe.Models.MessageModel

@{
    ViewBag.Title = "Send";
}

<div id="messageForm">

    <h2>Send Message</h2>

    @using (Ajax.BeginForm(new AjaxOptions
    {
        Confirm = "Are you sure?",
        HttpMethod = "Post",
        InsertionMode = InsertionMode.Replace,
        LoadingElementId = "loading",
        UpdateTargetId = "messageForm"
    }))
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)

        <fieldset>
            <legend>Message Model</legend>

            <div class="editor-label">
                @Html.LabelFor(model => model.From)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.From)
                @Html.ValidationMessageFor(model => model.From)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.Email)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Email)
                @Html.ValidationMessageFor(model => model.Email)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.Subject)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Subject)
                @Html.ValidationMessageFor(model => model.Subject)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.Message)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Message)
                @Html.ValidationMessageFor(model => model.Message)
            </div>

            <p>
                <input type="submit" value="Send Message" />
            </p>
        </fieldset>
    }
</div>

<div id="loading" style="display: none">
    Sending message...
</div>

@section Scripts{
    @Scripts.Render("~/bundles/jqueryval")
}

1 个答案:

答案 0 :(得分:1)

以下是您的操作方法:

控制器:

public class MessageModel
{
    public string From { get; set; }
    public string Email { get; set; }
    public string Subject { get; set; }
    public string Message { get; set; }
}

public class HomeController : Controller
{
    [HttpPost]
    public string ProcessForm(MessageModel messageModel)
    {
        Thread.Sleep(15000);
        return "<h2>Customer updated successfully!</h2>";
    }

    public ActionResult Index20()
    {
        MessageModel messageModel = new MessageModel { Email = "email", From = "from", Message = "message", Subject = "subject" };
        return View(messageModel);
    }

查看:

@model Testy20161006.Controllers.MessageModel

@{
    ViewBag.Title = "Send";
}

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index20</title>
    <script src="~/Scripts/jquery-1.12.4.min.js"></script>
    <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
    <script type="text/javascript">
        function OnBegin() {
            $("#divMsg").append("<h3>Beginning Ajax request.</h3>");
        }
        function OnComplete() {
            $("#divMsg").append("<h3>Completing Ajax request.</h3>");
        }
        function OnSuccess() {
            $("#divMsg").append("<h3>Ajax request successful.</h3>");
        }
        function OnFailure() {
            $("#divMsg").append("<h3>Ajax request failed.</h3>");
        }
        $(function () {


            $("#divProgress").css("display","none");
        })
    </script>
</head>
<body>
    <div id="messageForm">

        <h2>Send Message</h2>

        @{AjaxOptions options = new AjaxOptions();
          options.HttpMethod = "POST";
          options.Confirm = "Do you wish to submit this form?";
          options.OnBegin = "OnBegin";
          options.OnComplete = "OnComplete";
          options.OnFailure = "OnFailure";
          options.OnSuccess = "OnSuccess";
          options.LoadingElementId = "divProgress";
          options.LoadingElementDuration = 1000;
          options.UpdateTargetId = "divResponse";
          options.InsertionMode = InsertionMode.InsertAfter;

          }

        @using (Ajax.BeginForm("ProcessForm", "Home", options))
        {
            @Html.AntiForgeryToken()
            @Html.ValidationSummary(true)

            <fieldset>
                <legend>Message Model</legend>

                <div class="editor-label">
                    @Html.LabelFor(model => model.From)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.From)
                    @Html.ValidationMessageFor(model => model.From)
                </div>

                <div class="editor-label">
                    @Html.LabelFor(model => model.Email)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Email)
                    @Html.ValidationMessageFor(model => model.Email)
                </div>

                <div class="editor-label">
                    @Html.LabelFor(model => model.Subject)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Subject)
                    @Html.ValidationMessageFor(model => model.Subject)
                </div>

                <div class="editor-label">
                    @Html.LabelFor(model => model.Message)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Message)
                    @Html.ValidationMessageFor(model => model.Message)
                </div>

                <p>
                    <input type="submit" value="Send Message" />
                </p>
            </fieldset>
        }
    </div>

    <br />
    <br />
    <div id="divProgress">
        @*<img src='<%= Url.Content("~/images/ProgressCircle.gif") %>' />*@
        <img  src="~/Images/untitled.png"  />
    </div>
    <div id="divResponse"></div>
    <div id="divMsg"></div>

    @*<img id="loading" src="~/Images/untitled.png" style="display: none" />*@
</body>
</html>

信用转到http://www.codeguru.com/csharp/.net/working-with-ajax-helper-in-asp.net-mvc.htm