ASP.NET MVC4无法通过DateTime列对webgrid进行排序

时间:2013-11-27 11:38:28

标签: c# asp.net-mvc-4 sorting razor webgrid

在我的应用程序中,我有一个定义为的webgrid:

@model IEnumerable<ConnectManager2Test.Models.JobIterationViewModel>
@{
ViewBag.Title = "Unscheduled Jobs list";

var webGrid = new WebGrid(Model, rowsPerPage: 12, canSort: true, canPage: true, ajaxUpdateContainerId: "gridholder", ajaxUpdateCallback: "loadDataGrid");
webGrid.Pager(WebGridPagerModes.NextPrevious);
}

<h2 class = "table-striped-header">Unscheduled Jobs</h2>
<div id="gridholder">
@using (Html.BeginForm(null, null, FormMethod.Post, new { @id = "webgridForm" })) 
{
@webGrid.GetHtml(tableStyle: "table table-hover", columns: new[]{
    webGrid.Column(header:"Job Number", columnName:"Jobnumber", format:@<a onclick="joblookup(@item.jobid)">@item.Jobnumber</a>),
    webGrid.Column(header:"Job Type", columnName:"Jobtype", format:@<div>@item.JobType</div>),
    webGrid.Column(header:"Duration (mins)", columnName:"Duration",format: @<div>@item.Duration</div>),
    webGrid.Column(header:"Target Date", columnName:"TargetDate",format:@<div>@item.Targetdt</div>),
     webGrid.Column(header:"Assigned To", columnName:"Assignedto",format:@<div>@item.Assignedto</div>),
 webGrid.Column(header:"Description", columnName:"Description",format:@<div id = @("jobDetailsCol" + @item.jobid) onclick="detailslookup(@item.jobid)" 
 link = @Url.Action("JobDescription", "JobLists", new { jobId = item.jobid })>
 @item.Description</div>),
 webGrid.Column(header:"Flag", columnName:"Flag",format:@<div>@item.Flag</div>),
 webGrid.Column(header:"Priority", columnName:"Priority",format:@<div>@item.Priority</div>),
 webGrid.Column(header:"Location", columnName:"Location",format:@<div>@item.Location</div>)
    }
                            )}
</div>

这是由模型类JobIterationViewModel填充的:

公共类JobIterationViewModel     {         public int id {get;组; }

    public int jobid { get; set; }

    [Required]
    [DataType(DataType.Text)]
    [DisplayName("Job Number")]
    public string Jobnumber { get; set; }

    [Required]
    [DataType(DataType.Text)]
    [DisplayName("Job Type")]
    public string JobType { get; set; }

    [Required]
    [DisplayName("Duration (minutes)")]
    public int Duration { get; set; }

    [DataType(DataType.DateTime)]
    [DisplayName("Scheduled Date")]
    public DateTime? Scheduleddt { get; set; }

    [Required]
    [DataType(DataType.DateTime)]
    [DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]
    [DisplayName("Target Date")]
    public DateTime Targetdt { get; set; }

    [DisplayName("Assigned to")]
    [DataType(DataType.Text)]
    public string Assignedto { get; set; }

    [Required]
    [DisplayName("Description")]
    [DataType(DataType.MultilineText)]
    public string Description { get; set; }

    [DisplayName("Flag")]
    [DataType(DataType.Text)]
    public string Flag { get; set; }

    [DisplayName("Priority")]
    [DataType(DataType.Text)]
    public string Priority { get; set; }

    [Required]
    [DataType(DataType.Date)]
    [DisplayName("Created Date")]
    public DateTime Createddt { get; set; }

    [Required]
    [DataType(DataType.Text)]
    [DisplayName("Created By")]
    public string Createdby { get; set; }

    [DisplayName("Location")]
    [DataType(DataType.Text)]
    public string Location { get; set; }

    [DisplayName("Outcome")]
    [DataType(DataType.Text)]
    public string Outcome { get; set; }

    [Required]
    [DataType(DataType.Date)]
    [DisplayName("Updated Time")]
    public DateTime Updateddt { get; set; }
}

控制器是:

public ActionResult UnscheduledJobs(int page = 1, string sort = "", string direction = "asc")
        {
            try
            {
                IEnumerable<Jobiteration> iterationList = CommonJobFuncs.GetAllJobIterations().Where(j => j.Scheduleddt == null && j.Status == JobIterationStatus.active.ToString());


                ViewBag.Sort = sort;
                ViewBag.Direction = direction;              
                return PartialView(iterationList.Select(i => JobIterationViewModelFiller.GetModel(i)).OrderBy(i => i.Targetdt).ToList());
            }
            catch (Exception ex)
            {
                return new MvcDebug().Exception(ex);
            }
        }

问题是除了“目标日期”列之外,每列都正确排序。当我点击目标日期标题时,它按升序对其进行排序,但是当我再次单击它时,它不会按降序排序,与其他所有列不同。 有任何想法吗?感谢

1 个答案:

答案 0 :(得分:0)

找到它是什么 - 在视图中,列名必须与模型中变量的名称匹配。所以改变这一行:

webGrid.Column(header:"Target Date", columnName:"TargetDate",format:@<div>@item.Targetdt</div>),

webGrid.Column(header:"Target Date", columnName:"Targetdt",format:@<div>@item.Targetdt</div>),

做了这个伎俩。