将参数从javascript传递到控制器

时间:2018-11-14 12:17:05

标签: javascript asp.net-mvc parameters

我的页面上有一个按钮,单击时我具有javascript功能 看起来像这样:

   function PrintPdf() {
    var caseSearch = new Object();
    caseSearch.CustId = $('#CustId').val();
    caseSearch.CustName = $('#CustName').val();
    caseSearch.CustAddress = $('#CustAddress').val();
    caseSearch.CustPhone = $('#CustPhone').val();
    caseSearch.CustMobile = $('#CustMobile').val();
    caseSearch.SubTypeId = $('#CaseTypesDropDown').val();
    caseSearch.CaseStatusId = $('#CaseStatusDropDown').val();
    caseSearch.CreatorId = $('#slcOperId').val();
    caseSearch.TechnicianId = $('#slcTechId').val();
    caseSearch.CallDateFrom = $("#callDatefrom").val();
    caseSearch.CallDateTo = $("#callDateto").val();
    caseSearch.VisitDateFrom = $("#visitDatefrom").val();
    caseSearch.VisitDateTo = $("#visitDateto").val();
    caseSearch.RowsCount = $("#CaseRowsDropDown").val();
var url =  "/Home/PrintPdf?CustId=" + caseSearch.CustId + "&CustName=" + caseSearch.CustName + "&CustAddress=" + caseSearch.CustAddress +
        "&CustPhone=" + caseSearch.CustPhone + "&CustMobile=" + caseSearch.CustMobile + "&SubTypeId=" + caseSearch.SubTypeId + "&CaseStatusId=" + caseSearch.CaseStatusId +
        "&CreatorId=" + caseSearch.CreatorId + "&TechnicianId=" + caseSearch.TechnicianId + "&CallDateFrom=" + caseSearch.CallDateFrom + "&CallDateTo=" + caseSearch.CallDateTo +
        "&VisitDateFrom=" + caseSearch.VisitDateFrom + "&VisitDateTo=" + caseSearch.VisitDateTo;

    window.open(url,'_blank');
}

除日期值外,所有参数均可正常使用。javascript正在发送正确的值。这是一个网址:http://localhost:X/Home/PrintPdf?CustId=&CustName=&CustAddress=&CustPhone=&CustMobile=&SubTypeId=0&CaseStatusId=0&CreatorId=0&TechnicianId=117&CallDateFrom=09/11/2018&CallDateTo=14/11/2018&VisitDateFrom=&VisitDateTo=

但是在我的PrintPdf方法中,我得到了不同的日期值。 Here

1 个答案:

答案 0 :(得分:0)

我建议将日期发送为自1970年1月1日(EPOCH)以来的秒数。像这样:

var caseSearch.CallDateFrom =
  Math.round(Date.parse($("#callDatefrom").val()) / 1000);

,然后将其转换回接收端的Date对象[或类似对象]。