これは少しのAjaxの仕事のように見えますが、あなたの仕事がどう動くかについていくつか考え直してください。
最初のものは最初のユーザーは、私はタスクを追加し、返すメソッドを持つ仕事をするために別のコントローラを作成します
public class RequestTaskController: Controller
{
public ActionResult LongRunningAction()
{
return View();
}
}
次のタスクを要求することができる場所私は、独自のコントローラでこれを置くあなたのビューを表示します部分的な結果
public class WorkController: Controller
{
[HttpPost]
public ActionResult CreateLongRunningTask(TaskRequestViewModel viewModel)
{
AddTask(viewModel);
return CurrentTasks();
}
}
public PartialViewResult AddTask(TaskRequestViewModel viewModel)
{
// I'm using Session here because its easy
var currentTasks = Session["tasks"] as ConcurrentDictionary<string, Task>??
new ConcurrentDictionary<string, Task>();
Task task;
if (!currentTasks.TryGetValue(viewModel.Key, out task))
{
task = new Task(); // what ever that is
currentTasks.TryAdd(key, task);
}
// do stuff with task like change progess, add payload, mark as complete
}
public PartialViewResult CurrentTasks()
{
ViewData.Model = Session["tasks"] as ConcurrentDictionary<string, Task>??
new ConcurrentDictionary<string, Task>();
// do stuff with task like change progess, add payload, mark as complete
return PartialView("_CurrentTasks");
}
部分図_CurrentTasks.cshtmlは閲覧/共有にあるであろうと、あなたが持っているとどのような進展、彼らは何をしているか、タスクリストアップします。
@using System.Collections.Concurrent
@using Your.Models
@model ConcurrentDictionary<string, Tasks>
<ul>
@if (Model.Count > 0)
{
foreach (var task in Model)
{
<li>@:task</li>
}
}
</ul>
だから、それを表示することに注意してください。今は、LongRunningActionビューにすべてをラップしてそこのどこかで、このようなことをしたいと思うようにします。
<div id="currentTasks" />
<script type="text/javascript">
var interval;
function loadPartialView() {
//Populate the contents of the placeholder with the result returned by the action method
$('#currentTasks').load('@Url.Action("CurrentTasks", "Work")', function() {
// maybe do something to links
});
}
$(function() {
loadPartialView();
interval = setInterval(loadPartialView, 5000);
});
</script>
うまくいけば、あなたはどこにいてもよいでしょう。
あなたのアクションはActionResultを返さなければならず、その 'return View();'行で発生します。その前に何も返されません。 – veblock