views:

277

answers:

3

How to handle dynamically generated form submit in asp.net mvc?

Form is dynamically created (number, order and type of elements are always different) and i have to handle it (store the data in the database) in the Controller of asp.net mvc (there is no viewstate). Type of input can be everything; hidden fields, radio buttons, check boxes, text inputs etc..

<% using (Html.BeginForm("AddAnswer","Research")){ %>

<%= Html.Hidden("page", ViewData["curentPage"]) %>

<% foreach (var item in Model){ %>

<span><%= Html.Encode(item.Text) %></span>
    <%= Html.ActionLink("Edit", "Edit", new {id=item.QuestionID}) %>
    |
    <%= Html.ActionLink("Details", "Details", new { id=item.QuestionID })%>

    <%switch (item.QuestionTipe.QuestionTipeID){

        case 4:%>
        <table>
            <%foreach (var offeredAnswer in item.OfferedAnswer) {%>
                <tr>
                    <td><%= Html.CheckBox("q" + item.QuestionID, false, new{ value = offeredAnswer.Number})%></td>
                    <td><%= offeredAnswer.Text%></td>
                </tr>
            <%}%>
        </table>
        <% break;

        case 1:%>
        <table>
            <% foreach (var offeredAnswer in item.OfferedAnswer) {%>
                <tr>
                    <td><%= Html.RadioButton("q" + item.QuestionID, false, new{ value = offeredAnswer.Number})%></td>
                    <td><%= offeredAnswer.Text%></td>
                </tr>
            <%}%>
        </table>

        <% break;

        case 2:%>
        <div style="width:220px; height:20px; padding-top:10px; padding-left:8px;">
            <%= Html.TextBox("q" + item.QuestionID, null, new { style = "width:200px;"})%>
        </div>
        <% break;

        case 3:%>
        <div style="width:220px;height:20px; padding-top:10px;padding-left:8px;">
            <div id="q<%= item.QuestionID %>" style="width:200px;" class="slider">
            </div>
            <%= Html.Hidden("q" + item.QuestionID, 0)%>
        </div>
        <% break;
    }%>
<%}%>

<p>
    <input type="submit" value="Sljedeća strana" />
</p>
<%}%>
+4  A: 

In your action method, you can access FormCollection parameter, from there, you can access all your passed in values from your submit action.

public ActionResult YourActionMethod(FormCollection form)
{

}
J.W.
and how would you handle that form collection if you do not know the size or elements inside it?
Ante B.
I think you can enumerate through this. If you pass in an array of the element, then you can bind directly to an array, which is even easier.
J.W.
Bind? I have dynamically generated (number, order and type of elements are always different) form that isn't bindable with any object.
Ante B.
He's telling you that you can take that collection, loop through it (doesn't matter what's in it) and assign each value into a new array.. i.e. bind.
madcolor
http://weblogs.asp.net/rrobbins/archive/2008/04/17/iterate-through-the-form-collection-in-asp-net-2-0.aspx
madcolor
Maybe, you can post your form code.
J.W.
+1  A: 

In order to best help you decide how to process the form, it may be helpful to have some additional information.

  • Something is making the decision to generate this form, what is doing that? What is it basing its rendering on?

  • Are there known variations of the form that can be accounted for, or are the elements truly independent of each other?

  • Are each of the elements themselves known? If so, is it possible to give them a consistent id/name so that they may be recognized on the server-side?

  • When you speak of "handling" the submission, what is the end goal that you'd like to achieve? For example, are you parsing the form to store in a database?

Jesse Squire
I have list of questions with different types of answers like textbox, checkbox, radio buttons.. etc. The problem is the handeling of the form collection because it sends list of values for checkbox items and single value for rest of them.
Ante B.
the list of questions is always different so i can9t name them the same.. i'm giveing them name "q" + question_number
Ante B.
The part that I'm still unclear on, and believe may help, is knowing what you're looking to do with the data after you process it. Specifically, I'm trying to determine just how much context knowledge you actually need to parse. Is it as simple as iterating over the form and taking the answer, verbatim, and storing it based on question number?
Jesse Squire
yes, question number is contained in form key and answer value is value of the key. The problem is coused by questions with multiple answers which generates string "4,5,8,9" or list of values which can't be prcessed like others answers. I don't want to hard code it because i could use it later in different tipe of answer.
Ante B.
What you may wish to consider doing is to place a hidden field in the form, with a well-known id that contains a delimited list of the question numbers that you rendered which will contain value lists. This hidden field would be populated dynamically at runtime, as you build the questions. On the server-side, as you walk through the questions that were submitted, check against the question numbers contained in the hidden field. If a question number is present there, then you know that you need to treat it's answer as a list of values and process it appropriately.
Jesse Squire
A: 
foreach (var key in form.AllKeys) {
                    var answers = form.GetValues(key);

                    if (answers.Count() > 1){
                        foreach (var value in answers)
                        {
                            ...
                        }
                    }

                    else
                    {
                        ...
                    }
}

This is very simple. I'm checking if there is multiple values for some of the answers in the form.

Ante B.