更新datepicker信息时刷新页面

时间:2015-03-17 23:07:19

标签: jquery asp.net asp.net-mvc razor

当我在两个日期选择器上更改日期时,我有这个问题刷新我的页面,就像这里一样。

@model AtcWebReports.Controllers.MczirCasesModel
<h2>Case Entries</h2>
<div id="dates">
    <label id="StartDate" style="width: 200px">Start Date: @Html.EditorFor(model => model.StartDate)</label>
    <label id="EndDate" style="width: 200px">End Date: @Html.EditorFor(model => model.EndDate)</label>
    <label id="showDone" style="width: 200px">Show Done: @Html.CheckBoxFor(model => model.ShowDone)</label>
</div>

型号:

public class MczirCasesModel
{
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime? StartDate { get; set; }

    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime? EndDate { get; set; }

    public bool ShowDone { get; set; }

    public List<MczirCase> CasesAddedToday;
    public List<MczirCaseQt> ProductsQty;
}

目标是发布StartDateEndDate,然后考虑这些值刷新页面,并更新Model

这是唯一的当前控制器方法:

public ViewResult MczirCasesResult(DateTime start, DateTime end)
    {
        MczirCasesModel model = new MczirCasesModel();

        model.StartDate = start;
        model.EndDate = end;

        var tmpCases = (from cs in db.View_ATC_MczirIn
                        //where cs.InDate >= StartDate
                        select cs).ToList();

        var cases = from cs in tmpCases
                    group cs by new { cs.CaseNumber, cs.PanNumber, cs.DueDate, cs.DaysUntilDue, cs.ShipDate, cs.DaysUntilShip, cs.IsRushCase } into g
                    select new MczirCase {
                        CaseNumber = g.Key.CaseNumber,

                        Products = String.Join(", ", g.Select(i => i.ProductID).Distinct().Select(b => b +
                            " (" + g.Where(c => c.ProductID == b).Sum(d => d.Quantity) + ")")), // idk... works tho

                        Quantity = g.Sum(c => c.Quantity),
                        PanNumber = g.Key.PanNumber,
                        DueDate = g.Key.DueDate,
                        DaysUntilDue = g.Key.DaysUntilDue,
                        ShipDate = g.Key.ShipDate,
                        DaysUntilShip = g.Key.DaysUntilShip,
                        IsRushCase = g.Key.IsRushCase,
                    };

        var productQuantities = from cs in tmpCases
                                group cs by cs.ProductID into g
                                select new MczirCaseQt
                                {
                                    ProductID = g.Key,
                                    Qt = g.Sum(a => a.Quantity)
                                };

        model.CasesAddedToday = cases.ToList();
        model.ProductsQty = productQuantities.ToList();

        return View("MczirCases", model);//, cases.ToList());
    }

我选择使用EditorFor,因此我可以在屏幕上显示日期时间选择器。 现在,只要更改其中一个字段,我就想用新的Model.StartDate和Model.EndDate更新我的页面。有任何想法吗? 我对桌面应用程序有很好的经验。但网络是未知的领域。

编辑: 好。我认为这个jquery代码可以帮助我:

<script type="text/javascript">
$(document).change(function () {
    var selectedStart = new Date($('#StartDate').val()).toJSON();
    var selectedEnd = new Date($('#EndDate').val()).toJSON();

    $.ajax({
        type: "POST",
        url: "/Mczir/UpdateFilters",
        data: { dateStart: selectedStart, dateEnd: selectedEnd },
        success: function () {
            //alert('success');
        },
        error: function (err, result) {
            //alert("not working");
        }
    });
});

它实际上调用UpdateFilters方法,但我仍然无法刷新页面:

[HttpPost]
    public ActionResult UpdateFilters(string dateStart, string dateEnd)
    {
        DateTime start = DateTime.ParseExact(dateStart.Substring(0,10), "yyyy-MM-dd", CultureInfo.InvariantCulture);
        DateTime end = DateTime.ParseExact(dateEnd.Substring(0, 10), "yyyy-MM-dd", CultureInfo.InvariantCulture);
        return MczirCasesResult(start, end);
    }

1 个答案:

答案 0 :(得分:1)

我通过在脚本上传递正确的参数来解决它。此外,我将我的重要内容改为部分视图。

剧本最终看起来像这样:

<script type="text/javascript">
    $(document).change(function () {
        var selectedStart = new Date($('#StartDate').val()).toJSON();
        var selectedEnd = new Date($('#EndDate').val()).toJSON();
        var selectedShow = $('#ShowDone').is(":checked");

        $("#casesDiv").load('@Url.Action("UpdateFilters", "Mczir", null)', { dateStart: selectedStart, dateEnd: selectedEnd, done: selectedShow }, function (data) {});
    });
</script>