参数字典包含空条目?

时间:2014-02-21 16:07:00

标签: c# asp.net-mvc

我有这个MVC网络应用程序。我正在努力获得一些记录。但是,显然系统没有看到我的一个参数。该项目的代码如下:

模特:

public class AuditInfo
{
    public List<AuditRecord> AuditRecords { get; set; }
    public int PageIndex { get; set; }
}

非常简单的模型,包含审计记录列表和页码的PageIndex。

控制者: 在对这些记录的第一组100个数据条目的初始拉取时,它调用此方法:

public ActionResult AuditHistory(String username)
    {
        SearchInfo searchInfo = new SearchInfo { UserName = username };

        User user = UserManager.GetUser(username);
        var filteredApps = UserManager.GetUserAppListFromAuditRecord(user);
        List<ApplicationListView> appList = filteredApps.Select(Mapper.DynamicMap<ApplicationListView>).ToList();
        List<SelectListItem> appsList = appList.Select(appItem => new SelectListItem
        {
            Text = appItem.Name,
            Value = appItem.ID.ToString()
        }).ToList();
        appsList.Insert(0, new SelectListItem { Text = String.Empty, Value = String.Empty });
        ViewData["UserAppList"] = appsList;
        var uName = searchInfo.UserName;
        ViewData["UName"] = uName;
        AuditInfo auditInfo = _userHelper.GetAuditInfo(searchInfo);
        Response.CacheControl = "no-cache";
        Response.AddHeader("Pragma", "no-cache");
        Response.Expires = -1;

        return PartialView(auditInfo);
    }

在下一次拉动时,它会调用此方法:

public ActionResult GetRows(String username, int? selectedAppId, int pageIndex)
    {
        SearchInfo searchInfo = new SearchInfo { UserName = username };
        AuditInfo auditRecord = _userHelper.GetAuditInfo(searchInfo, pageIndex, selectedAppId);

        return PartialView("_AuditDataTable", auditRecord);
    }

后端逻辑工作正常,因为它返回结果很好,我还有一个基于下拉列表的过滤器,它工作正常。

在主视图中我有:

@model Models.ViewModel.AuditInfo
@{
List<SelectListItem> userApps = (List<SelectListItem>)ViewData["UserAppList"];
var userName = ViewData["UName"].ToString();
var page = Model.PageIndex;
}


@Html.Label("Sort by Application:")
@Html.DropDownList("appID", userApps, new { id = "appID" })

@using (Ajax.BeginForm("GetRows", "User", new AjaxOptions() { InsertionMode = InsertionMode.Replace, UpdateTargetId = "audit_partial" }, new { id = "dropListSubmit" }))
{    
@Html.Hidden("username", userName, new { id = "user" })
@Html.Hidden("selectedAppId")
@Html.Hidden("pageIndex", Model.PageIndex)
}

<div id="audit_partial">
@Html.Partial("_AuditDataTable", Model)
</div>
@{int pageNumber = @Model.PageIndex;}
@{ if (pageNumber > 0)
{
@Ajax.ActionLink("Previous", "GetRows", "User", new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "dropListSubmit" }, new { id = "prevRecords" })
   pageNumber = @Model.PageIndex - 1;
}
}
@{ if (Model.AuditRecords.Count == 100)
{
@Ajax.ActionLink("Next", "GetRows", new { controller = "User" }, new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "audit_partial" }, new { id = "nextRecords" })
   pageNumber = @Model.PageIndex + 1;
}
}
<script type="text/javascript">
$(document).ready(function () {
    $().dataTable({
        "bFilter": true,
        "bPaginate": true,
        "bLengthChange": false,
        'iDisplayLength': 100,
        "bSort": false,
        "bInfo": false,
        "sDom": "lrti"
    });

    $('#appID').change(function () {
        var appIdValue = $("#appID").val();
        $("#selectedAppId").val(appIdValue);
        $('#dropListSubmit').submit();
    });
});
</script>

我看不出为什么它没有看到pageIndex。谢谢你的期待!

1 个答案:

答案 0 :(得分:2)

在这一行

@Ajax.ActionLink("Previous", "GetRows", "User", new AjaxOptions { InsertionMode = nsertionMode.Replace, UpdateTargetId = "dropListSubmit" }, new { id = "prevRecords" })

未提供路线值。 nextRecords也是如此。对于可以为空的参数,它很好,虽然我不认为它是故意的,但pageIndex将失败。要解决这个问题,只需提供必要的对象:

@Ajax.ActionLink("Previous", "GetRows", "User",
                 new { pageIndex = @Model.PageIndex - 1, username = userName },
                 new AjaxOptions {
                     InsertionMode = nsertionMode.Replace,
                     UpdateTargetId = "dropListSubmit" },
                 new { id = "prevRecords" })