在.NET Ajax Post

时间:2017-03-13 12:52:27

标签: javascript jquery asp.net ajax asp.net-mvc-5

我有这样的.NET Ajax表单:

@using (Ajax.BeginForm("ChangePassword", "User", new { area = "UserSettings" }, new
AjaxOptions
{
HttpMethod = "POST",
OnSuccess = "( function() { ShowMessage('Password successfully changed.'); } )" //This will execute once the Ajax call is finished.
}, null))
{
@Html.TextBoxFor(m => m.Password, new { placeholder = "Password", @class = "form-control", @type = "password" })<br />
@Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger", @style = "float:right;" })
@Html.TextBoxFor(m => m.PasswordConfirm, new { placeholder = "Confirm password", @class = "form-control", @type = "password" })<br />
@Html.ValidationMessageFor(model => model.PasswordConfirm, "", new { @class = "text-danger", @style = "float:right;" })
<button type="submit" class="btn btn-primary">Change Password</button>
}

注意OnSuccess部分,如果我这样做,我可以显示一个简单的js警告弹出窗口:

OnSuccess = "Alert('OK');" //This will execute once the Ajax call is finished.

我在jQuery中创建了一个简单的函数,一旦调用就像这样显示一条更奇特的消息:

  function ShowMessage(message) {
        $("#info").text(message);
        $("#info").fadeIn("slow");
        $('#info').show(0).delay(1500).fadeOut(400).hide(0);
    }

如果我尝试做这样的事情:

  OnSuccess = "ShowMessage('OK');"

什么都没发生......我在这里做错了什么?

P.S。我已经引用了所有必需的文件来完成这项工作,包括jQuery ..

P.S。 2 ... ShowMessage函数在jquery的$(document).ready函数中定义...也许我应该发布整个函数体来使这个工作或..?

这是我定义函数的地方:

  $(document).ready(function () {
        function ShowMessage(message) {
            $("#info").text(message);
            $("#info").fadeIn("slow");
            $('#info').show(0).delay(1500).fadeOut(400).hide(0);
        }
  });

Supraj,这是用于信息标签的HTML:

   <div id="info" class="alert"><a href="#" class="close">Close</a></div>

编辑:伙计们,我设法做到了这样:

OnSuccess = "$('#info').text('Success'); $('#info').fadeIn('slow'); $('#info').show(0).delay(1500).fadeOut(400).hide(0); " //This will execute once the Ajax call is finished.

但这看起来真的很难看......有什么方法可以让这个东西多线化吗?

0 个答案:

没有答案