1

I'm building a form, where the number of questions, or inputs on the form varies depending on the value in a database.Each input on the form is a radio type. The name of the tags are dynamic and are loaded from the database using @db.row.questionID which would look something like: <span name=@id> and equal a value of 1 through whatever queries were requested.

My issue is, i wrote the form using post, and i want to submit the values back into a separate database, but i dont know how to request multiple values, that changes dynamically based on query.

Sample code i wrote, it doesnt give me any errors, but it doesnt give me any results either.

    foreach(var prow in poll){
        var Question = prow.PollId;
        if (Request.Form["@prow.PollId"] == "A") {                
            int AnsA = row.ResultsA;
            AnsA = AnsA + 1;
            db.Execute("UPDATE Results SET ResultsA=@0 WHERE ResultsId=@1", AnsA, Question);
        }

i have also tried:

    if (Request["prow.PollId"] == "B") {
            int AnsB = row.ResultsB;
            AnsB += 1;
            db.Execute("UPDATE Results SET ResultsB=@0 WHERE ResultsId=@1", AnsB, prow.PollId);
        }
user3681384
  • 105
  • 9
  • 1
    "@prow.PollId" is a string, if you want to pass dynamic parameters, you must use like Request.Form[Question] – Nguyễn Huy Jun 10 '15 at 10:03
  • 1
    Interesting post. I didnt take into consideration the quotes being a string value. But then when i switched out with what you said, it gave me an error on submit/post that said `Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: The best overloaded method match for 'System.Collections.Specialized.NameValueCollection.this[string]' has some invalid arguments` – user3681384 Jun 10 '15 at 10:50

1 Answers1

0

Do you want to get value in form with dynamic inputs? If yes, you can try this:

 NameValueCollection nvc = Request.Form;
 foreach (var item in Request.Form.AllKeys)
 {
        //do something you want.
        // Examble : if(item == "A")// item will return name of input
        // Note: nvc[item] return value of input
 }

Update:

Request.Form.AllKeys will return all of input name in form.

We use foreach to lopp through colections of input name.

Use nvc[item] or Request.Form[item] to get value of input.

You can read this article :c#: get values posted from a form

Community
  • 1
  • 1
Nguyễn Huy
  • 227
  • 9
  • 19
  • I was really hopeful for this solution but i am getting a `CS0246` namespace error... something about compiling and not being able to locate reference. Any solutions? – user3681384 Jun 11 '15 at 22:14
  • can you post full page code? On your comment above, error is invalid arguments, maybe has something wrong when you coding. – Nguyễn Huy Jun 12 '15 at 02:26
  • Oh, i apologize for that. I was trying to use the `NameValueCollection` and it gave me an error that the reference didnt lead to anything. The `foreach` didnt give any errors, so that worked. But the value isnt storing in database, i wanted to double-check what the `nvc` looked like... – user3681384 Jun 12 '15 at 06:50
  • You should using namespace System.Collections.Specialized or you can use Request.Form[item] to get value of input instead of nvc. – Nguyễn Huy Jun 12 '15 at 07:08
  • Okay, sorry for going back and forth, i just require a LITTLE bit more info before i completely understand. i have a radio form, and the name of the input is `1` so when i say `if(item == "A")` that is supposed to mean: `if form.radio.1 value is equal to "A"` or does it mean `if radio on form is named: "A"`? The syntax is working, the code is well written, but the database still has a value of `0`, so nothing is being submitted. – user3681384 Jun 12 '15 at 07:18
  • Oh, uhh, for some reason, i couldnt call NameValueCollection without typing out the whole path that it referenced to. Thanks for listing the path. But the example you wrote gave an error. It's fine though, cause i read the html instead on render, and it was numbered in the names. – user3681384 Jun 12 '15 at 07:20
  • 1
    in my examble: if(item=="A") mean if radio on form is named:"A". If you want to check value of input instead name of input, you can use : if(Request.Form[item] == "A"). – Nguyễn Huy Jun 12 '15 at 07:37
  • Thanks again for help and resources. Everything is working great :D – user3681384 Jun 12 '15 at 08:40