我已经看了这么多关于这个主题的评论,并且我尝试了一些建议的方法来完成此操作,但我仍然无法访问后面的代码。错误消息随成功例程返回。我使用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 ...线,它永远不会到达那里。 任何帮助,将不胜感激。
你试图访问<%= RESOLVEURL(“〜/ Default.aspx的/ SearchFind“)%>以确保客户端可以访问它?如果没有,你能确保预期的数据被发布? – atoms
这是可行的。我在其他许多网页中都使用过这个。我知道的唯一方法是在通过Ajax发送之前将其写入控制台。另外,在我进入Ajax之前,我已经把它串起来了,但它没有改变任何东西。谢谢。 – Rick
看看我的答案在下面的链接将帮助: https://stackoverflow.com/questions/40957556/passing-data-from-view-to-partial-in-net-core/44831001#44831001 –