I would idealy like to be able to create an array around the det[x] and then use a foreach, can this easily be done/how? or is another approach better?
The inputs come from a table with fixed columns, the variable item are the rows. A user selects tasks to put time against, they may have 1 or 10 tasks for the week. det[0] is the first row, det[1] is the second, etc, etc.
When saving into a table each row will be saved as its own entry.
I've used a while statement an the task id and a counter run a loop until the id is null, then I know row are finished, this works.
Mike, thanks for your previous input, it got my brain working again, and helped derive this solution.
FYI - The 'reuseFnct.conv_dec' is a c# function to convert the time allocations to a dec, as they can come through as null if not entered.
while (Request["det["+i+"][id]"] != null) {
// <text>@Request["det["+i+"][id]"] : @Request["det["+i+"][task]"] : @Request["det["+i+"][status]"] : @Request["det["+i+"][projectid]"] <br /></text>
var taskid = Request["det["+i+"][id]"];
var task = Request["det["+i+"][task]"];
var status = Request["det["+i+"][status]"];
var mon = reuseFnct.conv_dec(Request["det["+i+"][mon]"]);
var tue = reuseFnct.conv_dec(Request["det["+i+"][tue]"]);
var wed = reuseFnct.conv_dec(Request["det["+i+"][wed]"]);
var thu = reuseFnct.conv_dec(Request["det["+i+"][thu]"]);
var fri = reuseFnct.conv_dec(Request["det["+i+"][fri]"]);
var sat = reuseFnct.conv_dec(Request["det["+i+"][sat]"]);
var sun = reuseFnct.conv_dec(Request["det["+i+"][sun]"]);
var sql = "INSERT INTO tblUserTimesheets (LoginID ,TaskID ,TaskName, StartWeekDate, EndWeekDate, Status ,Mon , Tue, Wed, Thu, Fri, Sat, Sun ,Author ,CreatedDate) VALUES (@0, @1, @2, @3, @4, @5, @6, @7, @8, @9, @10, @11, @12, @13, @14)";
db.Execute(sql, WebSecurity.CurrentUserName , taskid, task, startweek, endweek, status, mon, tue, wed, thu, fri, sat, sun, author, createddate);
i++;
}
Marked as answer by 2bitcoder on Apr 21, 2012 11:48 AM
2bitcoder
Member
99 Points
69 Posts
Best way to Post multiple user generated fields?
Apr 21, 2012 05:10 AM|LINK
Hello,
I have a form that allows for the cloning of fields.
When posted I get multiples of fields ready for insertion into my database.
I would idealy like to be able to create an array around the det[x] and then use a foreach, can this easily be done/how? or is another approach better?
Mikesdotnett...
All-Star
154927 Points
19867 Posts
Moderator
MVP
Re: Best way to Post multiple user generated fields?
Apr 21, 2012 06:50 AM|LINK
@{ if(IsPost){ foreach(string x in Request.Form){ if(x.StartsWith("det")){ <text>@x : @Request[x]<br /></text> } } } }Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
2bitcoder
Member
99 Points
69 Posts
Re: Best way to Post multiple user generated fields?
Apr 21, 2012 07:36 AM|LINK
and how would I group the data for one insert per group within the foreach eg (to cater for an unlimited number of groupings)
Grouping ->det[0]
det[1]
etc
Mikesdotnett...
All-Star
154927 Points
19867 Posts
Moderator
MVP
Re: Best way to Post multiple user generated fields?
Apr 21, 2012 09:19 AM|LINK
How are you generating your inputs and what does det[0], det[1] etc represent?
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
2bitcoder
Member
99 Points
69 Posts
Re: Best way to Post multiple user generated fields?
Apr 21, 2012 11:47 AM|LINK
The inputs come from a table with fixed columns, the variable item are the rows. A user selects tasks to put time against, they may have 1 or 10 tasks for the week. det[0] is the first row, det[1] is the second, etc, etc.
When saving into a table each row will be saved as its own entry.
I've used a while statement an the task id and a counter run a loop until the id is null, then I know row are finished, this works.
Mike, thanks for your previous input, it got my brain working again, and helped derive this solution.
FYI - The 'reuseFnct.conv_dec' is a c# function to convert the time allocations to a dec, as they can come through as null if not entered.
while (Request["det["+i+"][id]"] != null) { // <text>@Request["det["+i+"][id]"] : @Request["det["+i+"][task]"] : @Request["det["+i+"][status]"] : @Request["det["+i+"][projectid]"] <br /></text> var taskid = Request["det["+i+"][id]"]; var task = Request["det["+i+"][task]"]; var status = Request["det["+i+"][status]"]; var mon = reuseFnct.conv_dec(Request["det["+i+"][mon]"]); var tue = reuseFnct.conv_dec(Request["det["+i+"][tue]"]); var wed = reuseFnct.conv_dec(Request["det["+i+"][wed]"]); var thu = reuseFnct.conv_dec(Request["det["+i+"][thu]"]); var fri = reuseFnct.conv_dec(Request["det["+i+"][fri]"]); var sat = reuseFnct.conv_dec(Request["det["+i+"][sat]"]); var sun = reuseFnct.conv_dec(Request["det["+i+"][sun]"]); var sql = "INSERT INTO tblUserTimesheets (LoginID ,TaskID ,TaskName, StartWeekDate, EndWeekDate, Status ,Mon , Tue, Wed, Thu, Fri, Sat, Sun ,Author ,CreatedDate) VALUES (@0, @1, @2, @3, @4, @5, @6, @7, @8, @9, @10, @11, @12, @13, @14)"; db.Execute(sql, WebSecurity.CurrentUserName , taskid, task, startweek, endweek, status, mon, tue, wed, thu, fri, sat, sun, author, createddate); i++; }