当我调用ajax时,我的整个内容被加载到该div中

时间:2016-06-21 16:40:06

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

这是我的脚本代码,通过它我在ajax cal上向我的控制器发送数据所有东西都工作正常但问题是我的整个内容来自div我还想要启用滚动条,最后调用需要好的建议< / p>

 <script type="text/javascript">
        $(function () {
            $("#my-form").on("submit", function (e) {
                e.preventDefault();

                $.ajax({
                    url:this.action,
                    type:this.method,
                    data:$(this).serialize(),
                    success:function(data){
                        $("#result").html(data);
                    }
                });
            });
        });
    </script>

这是我的实际查看代码,我在其中应用聊天应用程序我也希望在10ms内我的页面自动刷新收件人结束消息可以尽快阅读

<div class="col-md-7">
    <section class="panel">
        <header class="panel-heading">
            Chat <span class="tools pull-right">
                <a href="javascript:;" class="fa fa-chevron-down"></a>
                <a href="javascript:;" class="fa fa-cog"></a>
                @*<a href="~/Views/Chat/Create.cshtml">~/Views/Chat/Create.cshtml</a>*@
                <a href="javascript:;" class="fa fa-times"></a>
            </span>
        </header>
        @using (Html.BeginForm("AllMessages","Chat",FormMethod.Post,new {id="my-form" }))
        {
            <div class="panel-body"  id="result">
                <div class="chat-conversation1" >
                    <div class="slimScrollDiv" >
                        <ul class="conversation-list" >
                            @for (int i = 0; i < msg.Count(); i++)
                            {
                                if (msg[i].sender_id == ViewBag.Reciever_id)
                                {
                                    <li class="clearfix">

                                        <div class="chat-avatar">
                                            <img src="@Url.Content("~/Content/images/" + System.IO.Path.GetFileName(ViewBag.RecieverImage))" alt="" height="50" width="40" class="img-rounded" />
                                            <i>@Html.Label(msg[i].datetime.ToString())</i>    @*time from db*@
                                        </div>
                                        <div class="conversation-text">
                                            <div class="ctext-wrap">
                                                <i> @ViewBag.RecieverName</i>
                                                <p>
                                                    @Html.Label(msg[i].messages)
                                                    </p>
                                                </div>
                                            </div>

                                        </li>
                                    }
                                    else if (msg[i].sender_id == ViewBag.SenderId)
                                    {

                                        <li class="clearfix odd" style="margin-left:10px;">
                                            <div class="chat-avatar">
                                                <img src="@Url.Content("~/Content/images/" + System.IO.Path.GetFileName(ViewBag.SenderImage))" alt="" height="50" width="40"  class="img-rounded" />
                                                <i>@Html.Label(msg[i].datetime.ToString())</i>
                                            </div>
                                            <div class="conversation-text">
                                                <div class="ctext-wrap">
                                                    <i>  @ViewBag.SenderName</i>
                                                    <p>
                                                        @Html.Label(msg[i].messages)
                                                    </p>
                                                </div>
                                            </div>

                                        </li>
                                    }
                                }
                            </ul>



                        </div>
                        <div class="row chatSendBox">
                            <div class="col-xs-10 ">
                                @Html.HiddenFor(model => model.reciever_id, new { @Value= ViewBag.Reciever_id })
                                @Html.HiddenFor(model => model.datetime, new { @Value = DateTime.Now })
                                @Html.EditorFor(model => model.messages, new { htmlAttributes = new { @class = "form-control", @placeholder = "Enter To Chat..." } })
                            </div>
                            <div class="col-xs-2 chat-send">
                                <input type="submit" value="Send" class="form-control btn btn-info" />
                            </div>
                        </div>
                    </div>
                </div>
            }
        </section>
    </div>

This is my actual page when i send message it looks like this

1 个答案:

答案 0 :(得分:1)

好吧,你没有发布我们真正需要的东西:你的控制器动作。但是,我的猜测是你在那里返回View。您需要返回PartialView。如果部分视图用作局部视图,则它只是部分视图。