How can I handle the dynamically generated form?

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

The form is dynamically created (number, order and type of items are always different) and I have to handle it (store data in database) in asp.net mvc controller (no widget). The input type can be anything; hidden fields, radio buttons, checkboxes, 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>
<%}%>

      

+2


source to share


3 answers


foreach (var key in form.AllKeys) {
                    var answers = form.GetValues(key);

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

                    else
                    {
                        ...
                    }
}

      



It's very simple. I am checking if there are multiple values ​​for some of the responses in the form.

0


source


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



public ActionResult YourActionMethod(FormCollection form)
{

}

      

+4


source


To best help you decide how to handle the form, it may be helpful to have more information.

  • Something decides to create this form, what does it do? What does it base on rendering?

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

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

  • When you talk about "processing" a view, what is the end goal that you would like to achieve? For example, are you parsing a form to be stored in a database?

+1


source







All Articles