【问题标题】:jquery Ajax call from view to controller method but not disply the view returned that method in mvcjquery Ajax 从视图调用控制器方法,但不显示在 mvc 中返回该方法的视图
【发布时间】:2015-04-21 09:22:04
【问题描述】:

我已将 Ajax 调用从 Index.cshtml 视图提供给 MasterList 控制器中存在的 PrintPreview 方法,并且我还传递了参数。 Index 方法也存在于 MasterList 控制器中但是 从 PrintPreview 方法调用返回视图时将转到 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);

    }

【问题讨论】:

  • 数据必须与预期的“形状”相匹配。您的MasterListDTO 课程是什么,您是否有发送的数据样本(例如来自 Fiddler2)?

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


【解决方案1】:

数据必须与预期的“形状”相匹配。您正在传递一个对象(具有 2 个属性),但它需要一个列表(即 JSON 数组)。

你应该改变你的帖子来传递一个数组:

data: passresult: [{ saleorderIdList: JSON.stringify(saleorder_id),
      orderIdList:JSON.stringify(order_id) }],

如果失败,只是作为测试,将您的操作更改为此并查看它是否被命中(对于您现有的数据):

public ActionResult PrintPreview(MasterListDTO passresult)

【讨论】:

  • public ActionResult PrintPreview(string saleorderIdList, string orderIdList)
  • 上面提到的 PrintPreview 方法的特征然后在那个方法中提到::: JavaScriptSerializer serializer = new JavaScriptSerializer(); IEnumerable saleOrderIds = new int[] { }; IEnumerable orderIds = new int[] { }; if (saleorderIdList != null) { saleOrderIds = serializer.Deserialize>(saleorderIdList); } if (orderIdList != null) { orderIds = serializer.Deserialize>(orderIdList); }
  • @Gomtesh Hatgine:使用像 Fiddle2 这样的工具来查看发送到您的服务器的实际请求(并在您的问题中发布原始服务器请求供我们查看)。问:您的PrintPreview 操作是否标有[Httppost]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-22
  • 2017-01-24
  • 2011-09-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多