我试图将公共字典从 c# 传递到 asp.net。
这是我的代码:
代码背后:
public Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
RetrieveDataField();
}
}
void RetrieveDataField()
{
DataDefinitionResponse[] _dr = _service.DataDefinitionList(_companyID);
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Type");
foreach (DataDefinitionResponse dr in _dr)
{
if (dr.Type != "Group" && dr.Type != "File")
{
DataRow row = dt.NewRow();
row["Name"] = dr.Name;
row["Type"] = dr.Type;
dt.Rows.Add(row);
if (dr.Type == "Dropdown")
{
string[] strSplit = dr.ListValue.Split('|');
List<string> lst = new List<string>();
foreach (string word in strSplit)
{
lst.Add(word);
}
dict.Add(dr.Name, lst);
}
}
}
ddlFieldName.DataSource = dt;
ddlFieldName.DataTextField = "Name";
ddlFieldName.DataValueField = "Type";
ddlFieldName.DataBind();
}
ASP:
<script type="text/javascript">
$(document).ready(function () {
alert(<%=dict%>); //Error here
$("#MainContent_ddlFieldName").live("change", function () {
$.ajax({
type: "POST",
url: "WebService/WebFunction.asmx/PopulateDropdown",
data: "{'dict': '" + 'a' + "'}",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (data) {
// data.d;
// $("#txtBLShipperContactNo").val(data.d);
alert(data.d);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Error Shipper: " + errorThrown);
}
});
});
});
</script>
有什么想法吗?