【问题标题】:Clicking on a link in html doesn't reroute me to the correct page单击 html 中的链接不会将我重新路由到正确的页面
【发布时间】:2019-04-10 10:52:43
【问题描述】:

在过去的 2 周里,我一直在努力让它发挥作用,但我似乎找不到问题所在。当我单击用户与之聊天时,似乎并没有在两者之间创建聊天室。我遵循了那个教程https://pusher.com/tutorials/chat-aspnet/,但编辑了一些东西,比如我的数据库。调试后,当我单击其中一个用户链接时,它似乎什么也没做。

我尝试过多次重命名所有内容并重新开始。

我认为这可能与 routeconfig 有关,或者它甚至没有在点击时调用 javascript gae。

它所做的只是将我当前的地址:localhost/chat 更改为 localhost/chat#

我的 javascript 视图:

Javascript:

 // get chat data
    function getChat(contact_id) {
        $.get("/contact/conversations/" + contact_id)
            .done(function(resp) {
                let chat_data = resp.data || [];
                loadChat(chat_data);
            });
    }

    //load chat data into view
    function loadChat(chat_data) {
        chat_data.forEach(function(data) {
            displayMessage(data);
        });

        $('.chat__body').show();
        $('.__no__chat__').hide();
    }

    // select contact to chat with
    $('.user__item').click(function(e) {
        e.preventDefault();
        currentContact = {
            id: $(this).data('contact-id'),
            name: $(this).data('contact-name'),
        };
        if (conversationChannelName) {
            pusher.unsubscribe(conversationChannelName);
        }
        conversationChannelName = getConvoChannel((@ViewBag.currentUser.Id * 1), (currentContact.id * 1));
        currentconversationChannel = pusher.subscribe(conversationChannelName);
        bind_client_events();

        $('#contacts').find('li').removeClass('active');
        $('#contacts .contact-' + currentContact.id).find('li').addClass('active');
        getChat(currentContact.id);
    });

    function getConvoChannel(user_id, contact_id) {
        if (user_id > contact_id) {
            return 'private-chat-' + contact_id + '-' + user_id;
        }
        return 'private-chat-' + user_id + '-' + contact_id;
    }

html

<div class="__no__chat__">
                                <p>Select a contact to chat with</p>
                            </div>
                            <div class="panel-body users__body">
                                <ul id="contacts" class="list-group">

                                    @foreach (var user in @ViewBag.allUsers)
                                    {
                                        <a class="user__item contact-@user.Id" href="#" data-contact-id="@user.Id" data-contact-name="@user.FirstName">
                                            <li>
                                                <div class="avatar">
                                                    <img src="@Url.Content("~/Content/no_avatar.png")">
                                                </div>
                                                <span>@user.FirstName</span>
                                                <div class="status-bar"></div>
                                            </li>
                                        </a>
                                    }
                                </ul>
                            </div>

路由配置:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");


            routes.MapRoute(
                name: "Home",
                url: "",
                defaults: new { controller = "Home", action = "Index" }
            );

            routes.MapRoute(
                name: "Login",
                url: "login",
                defaults: new { controller = "Auth", action = "Index" }
            );

            routes.MapRoute(
                name: "ChatRoom",
                url: "chat",
                defaults: new { controller = "Chat", action = "Index" }
            );

            routes.MapRoute(
                name: "GetContactConversations",
                url: "contact/conversations/{contact}",
                defaults: new { controller = "Chat", action = "ConversationWithContact", contact = "" }
            );

            routes.MapRoute(
                name: "PusherAuth",
                url: "pusher/auth",
                defaults: new { controller = "Auth", action = "AuthForChannel" }
            );

            routes.MapRoute(
                name: "SendMessage",
                url: "send_message",
                defaults: new { controller = "Chat", action = "SendMessage" }
            );

            routes.MapRoute(
                name: "MessageDelivered",
                url: "message_delivered/{message_id}",
                defaults: new { controller = "Chat", action = "MessageDelivered", message_id = "" }
            );

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Auth", action = "Index", id = UrlParameter.Optional }
            );

【问题讨论】:

    标签: javascript html asp.net-mvc function routes


    【解决方案1】:

    好的,如果没有发生任何事情,这听起来像是页面上的 JS 的问题。先排除这个。

    你在 JS 中做过调试输出吗?

    不过,首先,您能否包装您的点击事件,以便在文档准备好时进行设置。

    更新 - 还在section 代码中添加以确保在正确的时间执行所有操作(即在加载 JQuery 的包之后)

    在你看来:

    @section scripts {
        $(document).ready(function() {
           
            $('.user__item').click(function(e) {
                e.preventDefault(); 
                console.log("click registered!");   //also add this directly at the beginning of the click event so you can see if something happens (or not)
                //...your original code below... 
            });
        
        });
    }
    

    注意:@section scripts... 在您的视图中只能出现一次。

    在您的_Layout.cshtml 文件中:

        ....
        @Scripts.Render("~/bundles/jquery")
        @Scripts.Render("~/bundles/bootstrap")
        @RenderSection("scripts", required: false)
    </body>
    </html>
    

    【讨论】:

    • 这是您在上面发布的Javascript 代码的更改。您正在用$(document).ready(function() { ... }); 包装您拥有的$('.user__item').click 代码,以确保在点击绑定到您发送的html 代码中的项目之前页面已加载。在 Chrome 中,您可以通过右键单击页面然后选择“检查”来查看控制台
    • 啊好吧 - 情节变厚了。从它的声音来看,您没有连接 jQuery。在您的Shared/_Layout.cshtml 文件中,您应该有一个&lt;link ... /&gt; 对JQuery 的引用? ...或者可能是这样的参考:@Scripts.Render("~/bundles/jquery")
    • 好的,在/Scripts 中是否有jquery-xxxx.js 文件(其中“xxxx”是版本/缩小版本)?同样在/App_Start/BundleConfig.cs 中,您应该声明 jquery 包(可能是第一个)。
    • 好的,很好。对,在_Layout.cshtml 中,您在结束&lt;/body&gt; 标签之前有@RenderSection("scripts", required: false) 吗?如果没有,请添加它。然后,在 OP 中的 Javascript 上,用我在上面的答案中所做的编辑将其包装起来......
    • 整个&lt;script&gt;...&lt;/script&gt;标签应该被包装
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-17
    相关资源
    最近更新 更多