查看未使用数据表刷新Razor MVC中的以下帖子
本文关键字:MVC 未使用 数据表 刷新 Razor | 更新日期: 2023-09-27 18:26:46
根据主题。视图是这样的。
@using System.Globalization
@model IEnumerable<TaskEngine.WebUI.Models.TaskViewModel>
<script src="../../Scripts/progress-task.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
url: '@Url.Action("Index", "Home")',
data: { from: "10/01/2014", to: "10/14/2014" },
dataType: "html",
success: function () {
alert('Success');
},
error: function (request) {
alert(request.responseText);
}
});
});
</script>
<div class="container">
<div class="row">
<div class="span16">
<div id="sitename">
<a href="/" id="logo">xxxxxxxx</a>
<span class="name">Workbasket</span>
</div>
<div class="row">
<div class="span16">
<table class="table table-striped table-condensed" id="task-table">
<thead>
<tr>
<th class="left">Client</th>
<th class="left">Task</th>
<th class="left">State</th>
<th class="left">Assigned By</th>
<th class="left">Assigned To</th>
<th class="left">Date Opened</th>
<th class="left">Date Due</th>
@* <th class="left">Date Closed</th>*@
<th class="left">Outcomes</th>
</tr>
</thead>
<tbody>
@foreach (var task in Model)
{
<tr>
<td><span>@task.ClientId</span></td>
<td><span class="nowrap">@task.TaskDescription</span></td>
<td><span class="nowrap">@task.CurrentState</span></td>
<td><span >@task.AssignedBy.Replace("CORPORATE''", "").Replace(@".", " ")</span></td>
<td><span>@task.AssignedTo.Replace("CORPORATE''", "").Replace(@".", " ")</span></td>
<td><span>@task.DateOpened.ToString("dd/MM/yyyy HH:mm")</span></td>
<td><span>@task.DateDue.ToString("dd/MM/yyyy HH:mm")</span></td>
@* <td><span>@(task.DateClosed.HasValue ? task.DateClosed.Value.ToShortDateString() : " - ")</span></td>*@
<td>
<span class="nowrap">
@Html.DropDownList(
"Outcomes",
new SelectList(task.Outcomes, "Id", "Name"), "---Please Select ---",
new Dictionary<string, object>
{
{"data-case-id", @task.CaseId }, {"data-task-log-id", @task.TaskLogId}, {"data-client-id", @task.ClientId}
})
</span>
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
</div>
</div>
<div class="modal hide span8" id="complete-task-modal" tabindex="-1" role="dialog" aria-labelledby="complete-task-header" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="complete-task-header">Complete Task</h3>
</div>
<div class="modal-body">
<div class="row">
<div class="span8">
<div class="alert alert-info">
<label id="CurrentState"></label>
<label id="NewState"></label>
<label>Generated Tasks</label>
<ul id="task-list">
</ul>
</div>
</div>
</div>
<form id="form">
<input type="hidden" id="task-log-id" name="taskLogId" />
<input type="hidden" id="case-id" name="caseId" />
<input type="hidden" id="outcome-id" name="triggerId" />
<input type="hidden" id="client-id" name="clientId" />
<div id="popup">
</div>
</form>
</div>
<div class="modal-footer">
<button class="btn" id="close" data-dismiss="modal" aria-hidden="true">Go Back</button>
<button class="btn btn-primary" id="confirm-task">Confirm</button>
</div>
</div>
</div>
这是控制器方法。
public ActionResult Index(DateTime? from, DateTime? to)
{
var usergroups = GetGroups(HttpContext.User.Identity.Name);
var model = _taskLogger.GetTasks(from, to)
.Select(task => new TaskViewModel
{
TaskLogId = task.TaskLogId,
CaseId = task.CaseId,
ClientId = task.ClientId,
TaskDescription = task.Description,
AssignedBy = task.AssignedBy,
AssignedTo = task.AssignedTo.Trim(),
DateOpened = task.DateCreated,
DateClosed = task.DateClosed,
DateDue = task.DateDue
}).ToList()
.Where(x => IsAvailableToUser(x.AssignedTo, usergroups))
.OrderBy(x => x.DateDue);
foreach (var task in model)
{
var workflow = _workflowEngine.GetCase(task.CaseId);
task.CurrentState = workflow.State.ToNonPascalString();
task.Outcomes = workflow.GetPermittedTriggers().OrderBy(x => x.Name).ToList();
}
ModelState.Clear();
return View(model);
}
当模型在ajax帖子之后返回时,数据集与预期的不同,但是,在视图中的数据表中,它仍然显示旧数据。
在这个问题上做了一些谷歌搜索后,我尝试清除模型状态,但这没有什么区别,而且从我读到的内容来看,它似乎只会影响HTMLHelpers。
我不确定这是数据表的问题,还是视图本身的刷新问题。任何意见都将不胜感激。
Ajax调用保持在同一页面上。如果您想用public ActionResult Index(DateTime? from, DateTime? to)
返回的视图更新页面,则需要将其添加到成功回调中的DOM中
$.ajax({
url: '@Url.Action("Index", "Home")',
data: { from: "10/01/2014", to: "10/14/2014" },
dataType: "html",
success: function (data) {
$('#someElement').html(data); // add the returned html to the DOM
},
....
});
但是从控制器中的代码来看,它似乎与原始视图是相同的视图,所以也许你真的想重定向,而不是停留在同一页