【问题标题】:Why can't use beginform with ajax?为什么不能在 ajax 中使用 beginform?
【发布时间】:2020-01-27 15:08:17
【问题描述】:

我需要发送 id 我的加载页面,我如何发送 id 并加载另一个页面?

我的错误:未捕获的 SyntaxError:意外的字符串 我现在这个错误语法错误,但我什么都试过了,我是 ajax 和 mvc 的新手

这是我的代码:

           $.ajax({
             type: "POST",
             contentType: "application/json; charset=utf-8",
           url : "http://localhost:9000/api/Customer/CustomerList",
             success: function (data) {
                 $("#customerList").DataTable({
                     pageLength:50,
                     destroy: true,
                     "data": data,
                     "columns": [
                         { "data": "CustID"},
                         {
                             "data":   function send(data) {
                                 return "@using (Html.BeginForm("CustomerList", "Customers", FormMethod.Post, new { target = "_blank" })) {<input type='submit' value=" + data.custID + " />}";
                              }
                         }
                     ]
                 });
             },
             error: function (data) {
                 alert("CustomerList");
             }
         });

【问题讨论】:

    标签: c# ajax asp.net-mvc


    【解决方案1】:

    必须先解析 Razor 语法才能工作。如果您检查页面的输出,您将永远找不到“Html.BeginForm”标签/代码。

    您的代码显示您正在使用 ajax 请求获取 CustomerList。您可以简单地使用相同的技术在单击按钮时发布 id。

    return "<input type='submit' onClick='sendID(" + data.custID )'/>";
    

    sendID 函数可能看起来像这样

    函数发送ID(客户ID){

    $.ajax({
             type: "POST",
             data: {custID: customerID},
             contentType: "application/json; charset=utf-8",
             url : "http://localhost:9000/api/Customer/GetCustomer", // your endpoint url here
             success: function (data) {
                  // do something
             });
             },
             error: function (data) {
                 alert("Could not send customerid");
             }
         });
    

    }

    这是未经测试的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-01
      • 2011-04-29
      • 1970-01-01
      • 1970-01-01
      • 2020-08-21
      • 1970-01-01
      相关资源
      最近更新 更多