我已从 Index.cshtml View 向 MasterList Controller 中存在的 PrintPreview 方法进行 Ajax 调用,并且还传递了参数。 MasterList Controller 中也存在 Index 方法,但是当从 PrintPreview 方法返回 View 时,调用将转到 PrintPreview.cshtml 页面,但页面未加载,即未在浏览器中显示,而 Index.cshtml 页面在浏览器中显示,请帮忙。
$('#printbtn').click(function () {
$.ajax({
type: 'POST',
url: '@Url.Action("PrintPreview", "MasterList")',
data:
{ saleorderIdList: JSON.stringify(saleorder_id),
orderIdList:JSON.stringify(order_id) },
});
});
public ActionResult PrintPreview(string saleorderIdList, string orderIdList)
{
var locationIdOfLoginUser = Convert.ToInt32(Session["LocationId"]);
ViewBag.loccationName = Session["LocationName"];
ViewBag.locationType = Session["LocationType"];
JavaScriptSerializer serializer = new JavaScriptSerializer();
IEnumerable<int> saleOrderIds = new int[] { };
IEnumerable<int> orderIds = new int[] { };
if (saleorderIdList != null)
{
saleOrderIds = serializer.Deserialize<IEnumerable<int>>(saleorderIdList);
}
if (orderIdList != null)
{
orderIds = serializer.Deserialize<IEnumerable<int>>(orderIdList);
}
MasterListService masterListService = new MasterListService();
var ordercollection = masterListService.GetSelectedorders(locationIdOfLoginUser, saleOrderIds, orderIds);
return View(ordercollection);
}
在上面的代码中我得到了数组 saleOrderIds 和 orderIds 也在 ordercollection 中我得到了预期的列表以供查看
调用将转到该 PrintPreview View ,也在该 View 中,当我传递给该 View 时,我得到了所有列表,但 PrintPreview.cshtml 页面未显示,而是从传递 ajax 调用的位置显示 Index.cshtml 页面
最佳答案
您正在做的是,您正在调用一个函数,并且不对返回值执行任何操作。您的 Ajax 调用应如下所示:
$('#printbtn').click(function () {
$.ajax({
type: 'POST',
url: '@Url.Action("PrintPreview", "MasterList")',
data:
{ saleorderIdList: JSON.stringify(saleorder_id),
orderIdList:JSON.stringify(order_id) },
onSuccess: function(response) {
// THIS IS WHERE YOU DISPLAY THE RETURN CSHTML SOMEWHERE. response RETURN VALUE IS NOTHING BUTH THE WHOLE CSHTML
//you may do something like
$('#mydiv').html(response);
}
});
});
假设您的网址和后端代码工作正常,这将工作正常。
关于jquery Ajax从 View 到 Controller 方法调用,但不显示在mvc中返回该方法的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29787923/