Telerik DatePicker在帖子上返回null

时间:2013-07-10 13:55:43

标签: c# javascript asp.net-mvc razor telerik-mvc

我有2个Telerik DatePicker用于过滤我的模型。但是我遇到的问题是部分视图的动作调用,两个日期都为空。我使用了注释掉的警报来确保日期实际上已经注册了。所以我认为这个问题与回发功能有关。

视图:

<script type="text/javascript">

    function filterChange() {
        $("#log").ajaxError(function (event, jqxhr, settings, exception) {
            alert(exception);
        });

        var startValue = $('#filterStart').data('tDatePicker').value();
        var endValue = $('#filterEnd').data('tDatePicker').value(); 

        //alert('Index: ' + startValue + ',' + endValue);


        $.get('@Url.Action("DashboardPartial")',
        {  start: startValue, end: endValue
        }, function (data) {           
            $("#target").html(data);
        });

    }
</script>

<div style="width: 100%; height: 100%">
    <fieldset>     
        <legend>Filters</legend>
        <div>
            @using (Html.BeginForm())
            {                
            <div class="Filter-Div">                      
                    @Html.Telerik().DatePicker().Name("filterStart").Value((DateTime)@ViewBag.StartDate).ClientEvents(events => events.OnChange("filterChange"))             

            </div>
            <div class="Filter-Div">                  
                    @Html.Telerik().DatePicker().Name("filterEnd").Value((DateTime)@ViewBag.EndDate).ClientEvents(events => events.OnChange("filterChange")).TodayButton()

            </div>                
            }

        </div>
    </fieldset>  
    @(Html.Telerik().ScriptRegistrar()
    .DefaultGroup(group => group
                .Add("telerik.common.js")
                .Add("telerik.tabstrip.min.js")
                .Add("telerik.calendar.min.js"))
    .jQuery(false))  

<div id="target">
   @{ Html.RenderPartial("DashboardPartial"); }
</div>

控制器

[OutputCache(Duration=5)]
        public PartialViewResult DashboardPartial( DateTime? start, DateTime? end)
        {

            db = new DashboardEntities();

            ViewBag.StartDate = start;
            ViewBag.EndDate = end;              

            var job = db.Job.where(Job=>Job.startDate > start && Job.endDate < end);                      

            return PartialView(prj);
        }

1 个答案:

答案 0 :(得分:1)

您的Javascript代码类型为Date的startValue和endValue变量吗?如果是这种情况,请将它们转换为字符串,如下所示:

<script type="text/javascript">

    function filterChange() {
        $("#log").ajaxError(function (event, jqxhr, settings, exception) {
            alert(exception);
        });

        var startValue = $('#filterStart').data('tDatePicker').value().toLocaleString();
        var endValue = $('#filterEnd').data('tDatePicker').value().toLocaleString(); 

        //alert('Index: ' + startValue + ',' + endValue);


        $.get('@Url.Action("DashboardPartial")',
        {  start: startValue, end: endValue
        }, function (data) {           
            $("#target").html(data);
        });

    }
</script>

此外,在您的表单中,在ViewBag.StartDate和ViewBag.EndDate之前删除“@”:

<div class="Filter-Div">                      
 @Html.Telerik().DatePicker().Name("filterStart").Value((DateTime)ViewBag.StartDate).ClientEvents(events => events.OnChange("filterChange"))             
</div>
<div class="Filter-Div">                  
 @Html.Telerik().DatePicker().Name("filterEnd").Value((DateTime)ViewBag.EndDate).ClientEvents(events => events.OnChange("filterChange")).TodayButton()
</div>