【问题标题】:How to trigger controller and open new page (along with model) with ajax如何使用 ajax 触发控制器并打开新页面(连同模型)
【发布时间】:2021-07-21 09:57:06
【问题描述】:

我有一个链接。它触发了我的ajax。我想在这个触发器之后打开一个新页面。 但它在触发后返回到我的ajax。有我的代码。

HTML

<a onclick="SiparisYazdir(@item.id)" >Print</a>

ajax

function SiparisYazdir(id)
    {
      $.ajax({
          url: '/Order/Print',
          type: 'GET',
          data: { "value": value, 'id': id } // value is taking from another method.
      });        
    }

控制器

public IActionResult Print(int value, int id)
        {
            //taking model here
            return View(model);
        }

【问题讨论】:

    标签: c# jquery ajax asp.net-core


    【解决方案1】:

    return View(model) 将返回视图的 html 内容 你不能把这个 html 放到你想要的标签中

    function SiparisYazdir(id)
        {
          $.ajax({
              url: '/Order/Print',
              type: 'GET',
              data: { "value": value, 'id': id } // value is taking from another method.,
              success:(result)=>{
                 $('your_desire_elemnt').html(result)
              }
          });        
        }
    
    

    【讨论】:

      【解决方案2】:

      如果你想重定向到另一个页面,我认为这里不需要ajax

      function SiparisYazdir(id) {
          window.location.href = "/Home/Privacy?value=" + value + "&id=" + id;
      }
      

      【讨论】:

      • 我现在不能试试这个。我不在电脑上。那么我可以用这个触发我的控制器吗?
      猜你喜欢
      • 2019-05-07
      • 2016-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多