我有一个页面,首先要求提供一些详细信息,然后在其下方的 div 中返回部分 View 。
这是我的 Ajax(自从我测试它以来,详细信息正在被获取并相应地传递给 Controller ):
<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>
这是 Controller (方法和变量均已正确处理):
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");
}
}
我调试了该函数,它正在进入 Controller 方法和部分 View 。唯一的问题是局部 View 当时没有显示在 View 中。我认为这与ajax有关!
这是应该显示它的 div:
<div id="divResult">test</div>
最佳答案
你的ajax调用的成功方法有一个错误。您需要将 divResult 更改为“#divResult”并将“data”附加到 html 而不是“success” 尝试这样的事情:
$.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);
}
});
关于jquery - MVC Ajax 返回部分 View 但未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23143816/