이것은 약간의 아약스에 대한 직업이며 직장이 어떻게 돌아가는지 다시 생각해 보입니다.
첫 번째 것들 먼저 사용자가 내가 작업을 추가하고 반환 방법과 작업을 수행하기 위해 다른 컨트롤러를 만들 것
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