简体   繁体   English

Asp.Net MVC 4,表单提交不起作用

[英]Asp.Net MVC 4, Form submit not working

I have a simple Asp.Net MVC 4 Chat application. 我有一个简单的Asp.Net MVC 4 Chat应用程序。 Below the chat session is initialized, I ask the user to enter the name and once the user enters his name (and other details not shown in below code) and presses on the "Start Chat" button, the Action method ChatForm is called from the Chat controller. 下面聊天会话初始化,我要求用户输入姓名,一旦用户输入他的名字(和其他细节在下面的代码未显示),并压在"Start Chat"按钮,操作方法ChatForm从所谓Chat控制器。

Code snippet for the chat form is as shown below: 聊天表单的代码段如下所示:

@model WebSite.ViewModel.SimpleForm

using (Html.BeginForm("ChatForm", "Chat", FormMethod.Post, new { id = "anonymousChat" }))
{
<div class='form-group'>
    @Html.LabelRequiredFor(m => m.Name)
    @Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
</div>
<input type="submit" class='btn btn-block start-anonymous-chat' value="Start Chat" />
}

The ChatForm method is as shown below: ChatForm方法如下所示:

public ActionResult ChatForm(SimpleForm model)
{
      /*Some processing is done here and finally the below values are returned*/

      return Json(new { success = true, chatId=chatRequest.Id, compId=company.Id });
}

Now the problem is after executing this function, i get the below line on the browser: 现在的问题是执行此功能后,我在浏览器上得到以下一行:

{"success":true,"chatId":10734,"compId":109}

The method executed on click of the submit button is: 单击提交按钮时执行的方法是:

$("#anonymousChat").submit(function () {
            debugger;
            if ($("#anonymousChat").valid())
                $.ajax({
                    type: this.method,
                    url: this.action,
                    data: $(this).serialize(),
                    success: function (data) {

                        if (data.success) {
                            /*show the chat modal*/
                            chatModal.modal("show");

                        }
                        else {
                            console.log("failed");
                            $("#anon-chat-form").html(data);
                        }
                    }
                });

            return false;
        });

Also if i try to put any alert message or console messages within the method $("#anonymousChat").submit , then neither the alter is popped-up nor the console messages or being printed. 另外,如果我尝试将任何警报消息或控制台消息放入方法$("#anonymousChat").submit ,则不会弹出更改,也不会弹出控制台消息或将其打印出来。

Has anyone been hit by similar problem before? 有人遇到过类似问题吗? Is so how did you solve it? 请问您是如何解决的?

Thanks in advance. 提前致谢。

The alert is not showing up, so the submit event is not triggered. 警报未显示,因此未触发Submit事件。

If your form is dynamically added to the DOM then the $("#anonymousChat").submit(function () {} won't trigger anything because the form does not exist yet when the page loads. Try changing your code like this to avoid this problem: 如果您的表单是动态添加到DOM的,则$("#anonymousChat").submit(function () {}不会触发任何操作,因为页面加载时表单尚不存在。请尝试将代码更改为避免这个问题:

$(document).on('submit', '#anonymousChat', function (e) {
  debugger;
        if ($("#anonymousChat").valid())
            $.ajax({
                type: this.method,
                url: this.action,
                data: $(this).serialize(),
                success: function (data) {

                    if (data.success) {
                        /*show the chat modal*/
                        chatModal.modal("show");

                    }
                    else {
                        console.log("failed");
                        $("#anon-chat-form").html(data);
                    }
                }
            });

        return false;
});

If it stil does not work maybe your problem is much simpler than that, maybe you just didn't reference the javascript file. 如果stil不起作用,则可能是您的问题比这简单得多,也许您只是未引用javascript文件。

您的表单ID为“ myChat”,但是您在JavaScript中引用的是“ anonymousChat”,请相信这可能会导致您的问题

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

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