反序列化jQuery序列化表格 [英] Deserialize jQuery Serialized Form
问题描述
我正在尝试将表单输入传递给WebMethod并执行某些操作. 我使用了 jQuery Serilize .
I'm trying to pass form inputs into a WebMethod and doing something. I used jQuery Serilize.
<script type="text/javascript">
$.fn.serializeNoViewState = function () {
return this.find("input,textarea,select,hidden")
.not("[type=hidden][name^=__]")
.serialize();
}
$(function () {
$("#Button1").click(function (e) {
var res = $("#myform").serializeNoViewState();
var jsonText = JSON.stringify({ bject: res });
$.ajax({
type: "POST",
url: "Default.aspx/Test",
data: jsonText,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
// alert("asd");
},
error: AjaxFailed
});
});
});
function AjaxFailed(result) {
alert("Failed");
}
</script>
在目标WebMethod中,我想反序列化通过的对象.
in target WebMethod I want to Deserialize that object I passed.
[WebMethod()]
public static bool test(string bject)
{
JavaScriptSerializer JsTool = new JavaScriptSerializer();
}
我试图使用Javascriptserilizer类.但我没有成功. 现在如何使用该对象? 我想使用这种方式来更简单地使用jQuery AJAX(例如,将表单输入传递给WebService并将其插入数据库中).由于我要执行的操作是这样吗? 欢迎您的建议和技巧.
I Tried to use Javascriptserilizer Class. but I did not succeed. now how can I use this Object? I want to use this way for using jQuery AJAX simpler(For example passing form inputs to a WebService and inserting that in Database). Due the action I want to do is this way right ? Welcome your Suggestions , tips .
如何将序列化JS对象映射到我的C#实体对象? 这是好方法吗?还是存在更好的方法?如果是,请给我一些信息
how can I map the Serialized JS object to my C# entity object? Is this way is good Way ? or there are better way exist ? if yes please give me some information
推荐答案
我建议您使用强类型.因此,定义一个包含所有必要属性的类:
I would recommend you working with strong types. So define a class that will contain all the necessary properties:
public class MyModel
{
public string Prop1 { get; set; }
public string Prop2 { get; set; }
}
,然后让您的网络方法使用此对象:
and then have your web method take this object:
[WebMethod()]
public static bool test(MyModel bject)
{
...
}
属性名称必须与您要在AJAX请求中序列化的输入字段名称匹配.
The name of the properties must match the input field names you are serializing in the AJAX request.
这篇关于反序列化jQuery序列化表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!