如何创建聊天窗口

时间:2021-01-04 20:46:27

标签: c# jquery asp.net model-view-controller

我可以通过表格显示消息。 我可以使用 textarea 吗?我不知道要使用哪些控件。 我是 MVC 的新手,任何帮助将不胜感激。

df['Team'] 
0       A
0       A
1       B
1       B
1       B

这是我的脚本:

<table class="table-responsive">
    <tr>
        <td id="output"></td>
    </tr>
</table>

1 个答案:

答案 0 :(得分:0)

("#output").html(output) - 输出是一个 <td>(表格数据),您正在向其中添加一个表格行。 HTML 结构应为 <table> <tr> <td>

因此,您很可能希望将 result 添加到 <table>

为了说明这一点,我制作了一个基本示例,其中变量 result 只是一个硬编码的 array(看起来像您的数据)。只需调用 forEach 并调用 append()<tr><td> 添加为字符串。

举个简单的例子,试试:

HTML:

<table  id="output" class="table-responsive">
</table>

脚本:

$(function() {
  const result = [ 
    { FromID: "FromID 1", "Body" : "Body 1", "DateSent" : "1/1/2021" }, 
    { FromID: "FromID 2", "Body" : "Body 2", "DateSent" : "2/2/2021" }, 
  ]

  result.forEach(i => {
    $("#output").append("<tr><td>"  + i.FromID + "/" + i.Body + "/" + i.DateSent + "</td></tr>");
  });
});

在此处查看演示:https://jsfiddle.net/dy3r0oqn/

没有足够的代码来测试您的解决方案。但是,您应该能够将代码与以下内容集成:

function sendName(id) {
    $.ajax({   
        datatype: "json",
        type: "POST",
        data: { 'senderName': id },
        url: '/Inbox/GetInboxes/',
        success: function (result)
        {
             result.forEach(i => {
                $("#output").append("<tr><td>"  + i.FromID + "/" + i.Body + "/" + i.DateSent + "</td></tr>");
            });
        },
        error: function (status, exception) 
        {
           //alert('Exception:', exception);
        }        
    });
}