使用数据表查看Razor MVC中的帖子后面没有刷新

时间:2014-10-15 13:26:14

标签: c# jquery ajax asp.net-mvc razor

根据主题。视图看起来像这样。

@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。

我不确定这是数据表的问题,还是视图本身的刷新问题。任何意见都将不胜感激。

1 个答案:

答案 0 :(得分:1)

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
  },
  ....
});

但是查看控制器中的代码它看起来与原始视图的视图相同,所以也许你真的想要重定向而不是停留在同一页面上