2017-07-24 60 views
0

我已經看了這麼多關於這個主題的評論,並且我嘗試了一些建議的方法來完成此操作,但我仍然無法訪問後面的代碼。錯誤消息隨成功例程返回。我使用Select2作爲輸入源(沒有列表,只有運營商希望輸入的內容),以便通過ajax將多個條目傳遞給我的代碼。我相信數據類型可能與背後的代碼不一樣,但不知道如何定義它,除了我所擁有的,或者它已被解釋並且我不知道。發生錯誤時,我更改爲原始編程。 myData更改爲模型以匹配後面的代碼。 這裏是我在select2中得到的東西,如果我輸入「test」和「copy」。將字符串從AJAX傳遞到後面的C​​#代碼

後模型值是:測試,複製

這裏是我的客戶端代碼:

... 
<select id="SearchSelect" style="width: 150px" name="SearchSelct" 
multiple onmouseover="SearchTip('#SrchToolTip','#hdnviewsrch');return 
false;"></select> 
    <textarea id="SrchToolTip" style="color:blue" hidden="hidden">You can 
enter any keyword you like.</textarea> 
    <br /> 
    <br /> 
<asp:Button ID="SearchFnd" runat="server" Text="Submit" 
OnClientClick="SearchSel('#SearchSelect');return false;" 
ValidateRequestMode="Disabled" UseSubmitBehavior="False"/> 

<script type="text/javascript"> 
    $(document).ready(function() { 
    //On client side - Here we render the Select 2 
    $("#SearchSelect").select2(
    { 
     placeholder: "Enter Search Parameters", 
     tags: true, 
     tokenSeparators: [','], 
     allowClear: true, 
     minimumInputLength: 1, 
     width: 'resolve', 
     }); 
    }); 
    $("#SearchSelect").on("change", function (e) { 
SearchChange("SearchSelect"); }); 

    function SearchTip(SrchElem) { 
     $(SrchElem).show(); 
     console.log("Search Tip value is: " + $("#SearchSelect").val()); 
     }; 

    function SearchChange(name, evt) { 
     console.log("Search Change value is: " + $("#SearchSelect").val() 
+ "; Name: " + name); 
    }; 
    function SearchSel(SchElem) { 
     var model= { "SrchData": $(SchElem).val() }; 
     $.ajax(
     { 
      type: "POST",  
      url: '<%= ResolveUrl("~/Default.aspx/SearchFind") %>', 
      data: JSON.stringify(model.SrchData), 
      dataType: 'json', 
      contentType: "application/json; charset=utf-8", 
      success: function (result) { 
       alert('success'); 
       console.log("Search data obtained: " + result); 
      }, 

      error: function (jqXHR, textStatus, errorThrown) { 
       alert("error: " + errorThrown); 
      }, 
      failure: function() { 
      alert('Failure'); 
      } 
      processResults: function (data, params) { 
       console.log("Search data obtained: " + data); 
       return { 
        results: data.d, 
        pagination: { 
         more: data.more 
        } 

       }; 
      } 
     }); 
    } 
    </script> 

這裏是後面的代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.Services; 
using System.Web.Script.Services; 

namespace WebApplication2 
{ 
    public partial class _Default : Page 
    { 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 
    public class Schdata 
    { 
     public string SrchData { get; set; } 
    } 

    [WebMethod()] 
    // [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public static string SearchFind(Schdata model) 
    { 
     System.Diagnostics.Debug.WriteLine("Went into the SearchFind 
     subroutine " + model.SrchData); 
     return "1"; 
     } 

    } 
} 

這裏是錯誤信息:

成功! {「消息」:「驗證失敗」,「堆棧跟蹤」:空,「ExceptionType」:「System.InvalidOperationException」}

該方案說,這是成功的,但給出了一個錯誤,該值爲null返回(或發送)但在後面的代碼我有一個斷點在System.Diagnostics.Debug.WriteLine ...線,它永遠不會到達那裏。 任何幫助,將不勝感激。

+0

你試圖訪問<%= RESOLVEURL(「〜/ Default.aspx的/ SearchFind「)%>以確保客戶端可以訪問它?如果沒有,你能確保預期的數據被髮布? – atoms

+0

這是可行的。我在其他許多網頁中都使用過這個。我知道的唯一方法是在通過Ajax發送之前將其寫入控制檯。另外,在我進入Ajax之前,我已經把它串起來了,但它沒有改變任何東西。謝謝。 – Rick

+0

看看我的答案在下面的鏈接將幫助: https://stackoverflow.com/questions/40957556/passing-data-from-view-to-partial-in-net-core/44831001#44831001 –

回答

0

在後面

public partial class _Default : Page 
{ 
    string myStrCb = "Value from back"; 
    protected void Page_Load(object sender, EventArgs e) 
    { 
    //... any code 
    } 
    // ...more code 
} 

你的代碼在你前面的代碼

<script> 
    var myJsVar = <%=myStrCb %> 
</script> 

使用腳本

function WriteValue() 
{ 
    console.log(myJsVar); // Value from back 
} 
+0

謝謝你花時間,但這不是我的問題。我可以從後端獲得我想要的任何數據,它從Ajax獲取值到後端是問題。我從AJAX收到成功,但它不會進入服務器端的子例程,並給我一個null值的錯誤消息。 – Rick

相關問題