我正在构建一个民意调查应用程序..用户输入一个问题,然后点击一个按钮动态创建答案的文本框..
然后我使用以下代码序列化表单:
var formData = $("#form1").find("input,textarea,select,hidden").not("#__VIEWSTATE,#__EVENTVALIDATION").serializeObject();
我使用jQuery.ajax使用data: formData
将序列化信息发送到WebMethod
这是我的问题开始的地方..如果它是一个静态表单,我的webmethod将是;
<WebMethod()>_
Public Shared Function addPoll(byval question as string, byval answer1 as string, etc...)
由于它们是动态的,我如何定义我的参数以及如何在webmethod函数中循环它们?
任何帮助将不胜感激......
答案 0 :(得分:0)
每个请求,一个使用通用列表传递数据作为参数的例子(如果我的VB.NET语法不符合要求,请原谅我,我多年来一直专注于C#)
首先,您要定义一个对象:
Public Class QA
Public Property Question() As String
Get
Return m_Question
End Get
Set
m_Question = Value
End Set
End Property
Private m_Question As String
Public Property Answer() As String
Get
Return m_Answer
End Get
Set
m_Answer = Value
End Set
End Property
Private m_Answer As String
End Class
然后定义您的webmethod:
<WebMethod> _
Protected Sub addPoll(args As IEnumerable(Of QA))
For Each qaPair As QA In args
'do stuff with qaPair
Next
End Sub
jQuery需要将此对象作为表示对象数组的JSON字符串传递给后端,如下所示:
[
{ Question: "what is the answer to life, the universe, and everything?",
Answer: "42" },
{ Question: "what is the average velocity of a laden swallow?",
Answer: "African or European?" }
]
可以使用JavaScriptSerializer解析为QA对象列表。
希望能为你清理它?