How do you Request[""] with a Dynamic Variable? Request["@Variable"]?

1k views Asked by At

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);
        }
1

There are 1 answers

8
Nguyễn Huy On BEST ANSWER

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