问题描述
我想更好地了解铸造对象的名称VS收藏价值的问题
i'd like to better understand the issue of casting object to a name vs value collection
说......只是如果我能做到这一点像
say ...just if i could do it like that
1)没有Java的脚本需要一些微调?打包的数据。
1) does the java-script needs some fine tuning ? packing the data..
2)对我来说最重要的是:什么是做从该键值的js
转换为<$ C的正确方法$ C>词典&LT; T,T&GT; C#?
2) and most important for me : What is the correct way to do the conversion from that key value Js
to a Dictionary<T,T> C#
?
在在.aspx / HTML
部分。
<input type="text" id="tbx_Name" value="Avi" />
<input type="text" id="tbx_City" value="TelAviv" />
<input type="text" id="tbx_Country" value="Israel" />
<select id="ChosenRandomClass" style="display:none">
<option selected="selected" value="0">(choose a random)</option>
<option value="1">random Top Beach</option>
<option value="2">random Top Center</option>
<option value="3">random Local Pub</option>
</select>
在的JavaScript / jQuery的
部分。
function AddNew() {
if (!confirm("would you like to add this contact ?")) return;
var Name = $('#tbx_Name').val();
var City = $('#tbx_City').val();
var Country = $('#tbx_Country').val();
var selectedRC = $('#ChosenRandomClass option:selected').val();
var hDate = []
var param1 = { key: "Name", value: Name };
var param2 = { key: "City", value: City };
var param3 = { key: "Country", value: Country };
var param4 = { key: "SelctedClass", value: selectedRC };
hDate.push(param1);
hDate.push(param2);
hDate.push(param3);
hDate.push(param4);
// is this part necessary the data will not get to
// code behind properly without the serializing ?
var startPrt = Sys.Serialization.JavaScriptSerializer.serialize(hDate);
ajaxUpdate("addNew", startPrt);
}
在 code后面的C#
部分。
public void AddNewRecord(object startPrt)
{
Dictionary<string, string> SenthDate = new Dictionary<string, string>();
// .....etc
}
我会的AP preciate正确的答案
i will appreciate the correct answer
感谢你的帮助和时间。
推荐答案
我给你的样品一试,发现在 startPrt 参数实际上是被接收为阵列中的词典(字符串对象)。因此,如果您的AJAX调用是这样的:
I gave your sample a try and noticed that the startPrt parameter is actually being received as an Array of Dictionary(string, object). Hence, if you make the AJAX call like this:
var hDate = [];
hDate.push({ key: "Name", value: Name });
hDate.push({ key: "City", value: City });
hDate.push({ key: "Country", value: Country });
hDate.push({ key: "SelctedClass", value: selectedRC });
$.ajax({
contentType: 'application/json; charset=utf-8',
url: 'MyPage.aspx/AddNewRecord',
type: 'POST',
data: JSON.stringify({ startPrt: hDate }),
success: success, // success callback
error: error // error callback
});
您可以定义您的的WebMethod 像follwoing转换的 startPrt 参数字典:
You can define your WebMethod like the follwoing to convert the startPrt parameter to a dictionary:
[WebMethod]
public static void AddNewRecord(object startPrt)
{
var dict = new Dictionary<string, object>();
foreach (Dictionary<string, object> pair in (Array)startPrt)
dict.Add((string)pair["key"], pair["value"]);
}
这篇关于试图将对象强制转换为JavaScript的[] {} - C#核心价值集合或字典&LT;字符串,对象&gt;或LT;字符串,字符串&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!