ajax控制器调用无法正常工作

时间:2014-10-28 06:42:00

标签: javascript jquery asp.net-mvc-4 asp.net-ajax actionresult

我遇到 ajax cal到控制器MVC 4 的问题,我必须传递下拉值。我使用js函数并传递给控制器使用ajax 。它正确传递但在操作之后,它无法进入成功或错误消息。 这是我的代码。

查看

    @model SCT.Models.ReportModel

@{
    ViewBag.Title = "Ticket Adders Report";
}

<h2>Ticket Adders Report</h2>


<div style="padding:10px;border:1px solid black">
    <table>
        <tr>
            <td>
                Store ID  :

                @{
                    List<SelectListItem> listItems = new List<SelectListItem>();
                    listItems.Add(new SelectListItem
                         {
                             Text = "101",
                             Value = "101"                           
                         });
                    listItems.Add(new SelectListItem
                         {
                             Text = "102",
                             Value = "102",

                         });
                    listItems.Add(new SelectListItem
                         {
                             Text = "103",
                             Value = "103"
                         });
                    listItems.Add(new SelectListItem
                    {
                        Text = "104",
                        Value = "104"

                    }); 

                }
                @Html.DropDownListFor(model => model.StoreId, listItems, "Please Select")


            </td>
            <td>           
                @*<div><a href="@Url.Action("ViewReport", new { id = "Image", }) ">View Report</a></div>*@  
                <div><a href="" onclick="viewreport('Image')">View Report</a></div>      
            </td>
        </tr>
    </table>
</div>

<script type="text/javascript">

    function viewreport(id) {
        debugger;
        $.ajaxSetup({
            jsonp: null,
            jsonpCallback: null
        });

        var StoreId = $("#StoreId").val();

        var params = { id: id, StoreId: StoreId };

        $.ajax({
            type: "POST", //or GET
            url: "@Url.Action("ViewReport", "Report")",
            data: JSON.stringify(params),
            cache : false,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                alert("success");
                //do something
            },
            error: function () {
                alert("failed");
            }
        });
    }

</script>

控制器操作

    public ActionResult ViewReport(string id,string StoreId)
        {

            LocalReport lr = new LocalReport();
            string path = Path.Combine(Server.MapPath("~/Reports"), "TicketAdders.rdlc");
            if (System.IO.File.Exists(path))
            {

                lr.ReportPath = path;
            }
....................
....................

            renderedBytes = lr.Render(reportType, deviceInfo, out mimeType, out encoding, out fileNameExtension, out streams, out warnings);

            //return File(renderedBytes, mimeType);
            return Json(renderedBytes);
}

调试时,一切都很好,但是无法打开报告。但是当我取消注释时:

@*<div><a href="@Url.Action("ViewReport", new { id = "Image", }) ">View Report</a></div>*@

报告已正确显示。我经常搜索,但找不到任何有用的东西。我觉得我在回归类型上有一些问题。但它应该是一个错误信息。请帮助。谢谢。

0 个答案:

没有答案