How to pass a dictionary <string, string> to a variable from code to asp.net?

I am trying to pass a public dictionary from C # to asp.net.

Here is my code:

Code for:

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> 

Any thoughts?

+4
source share
2 answers

When you use <%=dict%> , the compiler executes the default ToString () method by default, so you get something like alert(System.Collections.Generic.Dictionary...); . Note that there are no quotes around this, so it generates a javascript error. You can "fix" this by surrounding it in quotation marks: alert("<%=dict%>"); .

However, this is probably not what you want to do. You are most likely trying to get the actual dictionary into a Javascript object. To do this, you can use javascript JSON.parse and System.Web.Script.Serialization.JavaScriptSerializer to output data as a JSON object. If you use jQuery, you can use parseJSON, otherwise you can use JSON.parse in most browsers, I think (I always use jQuery, so I'm more familiar with it than the browser-provided JSON.parse).

Here is the code to output the object in JSON:

 public string DictJSON { get { JavaScriptSerializer jSer = new JavaScriptSerializer(); return jSer.Serialize(dict); } } 

and here is how you can use it using jQuery:

 $.parseJSON('<%=DictJSON %>'); 

Please note that JavaScriptSerializer is part of System.Web.Extensions.dll, so you need to add a link to it. It cannot handle all types of objects, but the dictionary should be in order, like List. Also, since it prints double-quoted strings of JSON objects, make sure you use single quotes in parseJSON.

+4
source

I really don't know the error generated by your codes, but here is what I know. The dictionary should be added or β€œlinked” as follows:

 DataTable dt = new DataTable(); Dictionary<string,string> dict = new Dictionary<string,string>(); dt.Columns.Add(new DataColumn("Name", typeof(string))); dt.Columns.Add(new DataColumn("Type", typeof(string))); foreach (DataDefinitionResponse dr in _dr) { if (dr.Type != "Group" && dr.Type != "File") { DataRow row = dt.NewRow(); row["Name"] = dr["Name"].toString(); row["Type"] = dr["Key"].toString; dt.Rows.Add(row); } } foreach(DataRow dr in dt.Rows) { dict.Add(dr.ItemArray[0].ToString(), dr.ItemArray[1].ToString()); } ddlFieldName.DataSource = dict; // the dictionary should be here ddlFieldName.DataTextField = "Key"; ddlFieldName.DataValueField = "Value"; ddlFieldName.DataBind(); 

or add a dictionary element inside the 1st foreach loop as follows:

 foreach (DataDefinitionResponse dr in _dr) { if (dr.Type != "Group" && dr.Type != "File") { DataRow row = dt.NewRow(); row["Name"] = dr["Name"].toString(); row["Type"] = dr["Key"].toString(); dt.Rows.Add(row); dict.Add(dr["Key"].ToString(), dr["Name"].ToString()); } } ddlFieldName.DataSource = dict; // the dictionary should be here ddlFieldName.DataTextField = "Key"; ddlFieldName.DataValueField = "Value"; ddlFieldName.DataBind(); 

UPDATE

You declare or initialize your column in the wrong way. You can try my update.

+1
source

Source: https://habr.com/ru/post/1489825/


All Articles