MVC Ajax返回部分视图但它没有显示

时间:2014-04-17 21:30:49

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

我有一个页面首先要求提供一些细节,然后在它下面的div中返回一个局部视图。

这是我的Ajax(自我测试以来,正在采取细节并将其传递给控制器​​)​​:

<script type="text/javascript">
function Filter() {
    var startd = $('#startdt').val();
    var endd = $('#enddt').val();
    var itemname = $("#Products option:selected").val();

    var param = { StartD: startd, EndD: endd, ItemName: itemname };

    $.ajax({
        url: '@Url.Action("FilterTransaction")',
        type: "POST",
        datatype: "html",
        UpdateTargetId: "divResult",
        data: param,
        error: function (xmlHttpRequest, errorText, thrownError) {
            alert(xmlHttpRequest + "|" + errorText + "|" + thrownError);
        },
        success: function (data) {
            $('divResult').html(result);
        }
    });
}
</script>

这是控制器(方法和变量都经过正确处理):

public PartialViewResult FilterTransaction(string StartD, string EndD, string ItemName)
    {
        try
        {
            int productid = new ProductService.ProductsServiceClient().GetProductID(ItemName);
            Product p = new ProductService.ProductsServiceClient().GetProduct(productid);
            ViewBag.ItemNo = productid;
            ViewBag.ItemName = p.Name;
            ViewBag.AmmountP = p.Price;
            int orderid = new OrderService.OrderServiceClient().GetOrderID(productid, User.Identity.Name);
            Order o = new OrderService.OrderServiceClient().GetOrder(orderid);
            ViewBag.DatePurchased = o.Date;
            ViewBag.WarrantyExpiryDate = o.Date.AddYears(2);

            DateTime fromd = Convert.ToDateTime(StartD);
            DateTime todate = Convert.ToDateTime(EndD);
            todate = todate.AddDays(1);

            List<PrintViewFD> printv = new List<PrintViewFD>();
            foreach (FaultDetail fs in new FaultService.FaultServiceClient().GetListOfFaultDetailsbetweenDATES(orderid, productid, fromd, todate))
            {
                PrintViewFD pvd = new PrintViewFD();
                pvd.date = fs.Date.ToString();
                pvd.details = fs.Details;
                pvd.faultid = fs.FaultID;
                FaultStatu fss = new FaultService.FaultServiceClient().GetStatus(fs.FaultStatusID);
                pvd.status = fss.Status;
                printv.Add(pvd);
            }

            ViewBag.FaultDetailsLIST = printv;
            return PartialView("_filtertransactions");
        }
        catch
        {
            return PartialView("_filtertransactions");
        }
    }

我调试了该函数,它正在输入控制器方法和部分视图。唯一的问题是视图中没有显示局部视图。我认为这与ajax有关!

这是应该显示它的div:

<div id="divResult">test</div>

1 个答案:

答案 0 :(得分:2)

你的ajax调用成功方法出了问题。您需要将divResult更改为&#34; #divResult&#34;并追加数据&#39;到html而不是&#39;成功&#39; 尝试这样的事情:

$.ajax({
    url: '@Url.Action("FilterTransaction")',
    type: "POST",
    datatype: "html",
    UpdateTargetId: "divResult",
    data: param,
    error: function (xmlHttpRequest, errorText, thrownError) {
        alert(xmlHttpRequest + "|" + errorText + "|" + thrownError);
    },
    success: function (data) {
        $("#divResult").empty(); //In case you're going to refresh this multiple times
        $("#divResult").html(data);
    }
});