我正在尝试将表单数据发送到确认页面,您可以在其中输入cc信息以完成交易。我将所有信息添加到列表中,稍后我将在新表单上将其编码为隐藏字段,以便在付费时发送信息。但是,当我执行repsonse.write以查看存储的信息时。我总是一遍又一遍地说出同样的名字。不确定我错过了哪里。谢谢你的帮助!
protected void Page_Load(object sender, EventArgs e)
{
if (Request.HttpMethod == "POST"){
formData attendee = new formData();
NameValueCollection nvc = Request.Form;
List<formData> attendees = new List<formData>();
if (!string.IsNullOrEmpty(nvc["fn1"]))
{
var fn = nvc["fn1"];
var ln = nvc["ln1"];
var email = nvc["email1"];
var wife = nvc["wife1"];
var luncheon = nvc["lucheon1"];
attendee.firstName = fn; attendee.lastName = ln; attendee.email = email; attendee.wife = wife; attendee.luncheon = luncheon;
attendees.Add(attendee);
}
if (!string.IsNullOrEmpty(nvc["fn2"]))
{
var fn = nvc["fn2"];
var ln = nvc["ln2"];
var email = nvc["email2"];
var wife = nvc["wife2"];
var luncheon = nvc["lucheon2"];
attendee.firstName = fn; attendee.lastName = ln; attendee.email = email; attendee.wife = wife; attendee.luncheon = luncheon;
attendees.Add(attendee);
}
foreach(var person in attendees)
{
Response.Write(person.firstName.ToString());
Response.Write(person.lastName.ToString());
Response.Write(person.email.ToString());
Response.Write(person.wife.ToString());
Response.Write(person.luncheon.ToString());
}
}
}
public class formData
{
public string firstName { get; set; }
public string lastName { get; set; }
public string email { get; set; }
public string wife { get; set; }
public string luncheon { get; set; }
}
}
答案 0 :(得分:0)
我假设您的参与者数组将是这样的数据类型......每个参与者都有{fn,ln,email,wife和luncheon}属性。看起来它们存储在POST中,如{fn1,ln1,email1,wife1,luncheon1}是与会者(0),{fn2,ln2,email2,wife2,luncheon2}是与会者(1)等。
您可以尝试像这样循环
var count = 1;
while (!string.IsNullOrEmpty(nvc["fn" + count]))
{
attendee(count).firstname=nvc["fn" + count]
attendee(count).lastname=nvc["ln" + count]
//...for the rest of the fields
count++; //be sure and increment count
}
在数据fn#为null之前,它会不断循环并创建与会者。
答案 1 :(得分:0)
您正在Page_Load事件中创建列表。每次页面加载,回发或不回发时都会触发Page_Load事件,并在每次事件触发时重新创建列表。
您可以创建和使用会话变量。举个例子:
protected void Page_Load(object sender, EventArgs e)
{
List<formData> attendees;
if(Session["attendees"] == null)
{
attendees = new List<formData>();
// your logic logic ...
Session["attendees"] = attendees;
}
else
{
var attendees = (List<formData>)Session["attendees"];
// your logic ...
Session["attendees"] = attendees;
}
}
我还考虑稍微破解你的代码。也许将添加到列表中的代码放在一个单独的方法中。