我想用2个局部视图创建视图: 首先是整个对话,其次是允许发送新消息的表格
第一个视图应该每X秒刷新一次(比如说5秒),但是此时用户应该可以在
在控制器(消息)中,我有以下方法:
$arr = [
['um' => 1000, 'kali' => '2'],
['um' => 2000, 'kali' => '5'],
['um' => 5000, 'kali' => '1']
];
$res = [];
array_walk($arr, function($v, $k) use (&$res){
$condition = ($v['kali'] == 5) ? 4 : ($v['kali']);
for($i=1;$i <= $condition;$i++){
$res[] = ['um' => $v['um'], 'kali' => count($res)+1];
}
});
我有以下观点 详细信息:
[HttpGet]
public ViewResult Index()
{
var vm = _manager.GetAllConversations();
return View(vm);
}
[HttpGet]
public ViewResult Details(int speakerId)
{
_speakerId = speakerId;
var vm = _manager.GetConversation(_speakerId);
return View(vm);
}
[HttpPost]
public IActionResult SendMessage(SendMessageViewModel vm)
{
_manager.SendMessage(vm.SpeakerId, vm.Content);
return RedirectToAction("Details", vm.SpeakerId);
}
和部分视图SendMessage:
<div class="col-md-6" id="conversationDiv">
<script type="text/javascript">
setInterval("$('#conversationDiv').load('Messages/Details/1')", 2000);
</script>
@foreach (var speak in Model)
{
<div>@speak.SendDate @speak.SenderName</div>
<div>@speak.Message</div>
<hr />
}
</div>
<div>
@{
await Html.RenderPartialAsync("SendMessage",
new RandevouMVC.ViewModels.Messages.SendMessageViewModel());
}
</div>
但是JQuery刷新脚本不起作用...
在Shared / _Layout.cshtml中,我有:
<form asp-controller="Messages" asp-action="SendMessage" method="post">
<textarea asp-for="Content" class="form-control" rows="4" placeholder="message content...">
</textarea>
<button type="submit" class="btn btn-primary">Send</button>
</form>
但是jquery尝试不加载
<environment include="Development">
<script src="~/lib/jquery/dist/jquery.js"></script>
但是:
localhost:port/messages/details/1
答案 0 :(得分:1)
更改
setInterval("$('#conversationDiv').load('Messages/Details/1')", 2000);
进入
setInterval("$('#conversationDiv').load('/Messages/Details/1')", 2000);
在前面加一个斜杠以确保URL是从网站的根目录加载的。