0

I have a web project that I want to create text box dynamically and a Button for every text box that upload a file and I have below code, my problem is I get null when check files in my controllers, I think I must put a array file to get but I don't know why it doesn't work

Thank you

my cshtml

<h2>اضافه کردن سؤال جدید</h2>

@using (Html.BeginForm("AddNewQuestion", "BestDestinationResult", FormMethod.Post,
                            new { enctype = "multipart/form-data" }))
{

    <input id="btnAdd" type="button" value="اضافه کردن پاسخ" onclick="AddTextBox()" />
    <br />
    <br />
    <div id="TextBoxContainer">
        <!--Textboxes will be added here -->
    </div>
    <br />

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
    function GetDynamicTextBox(value) {
        var div = $("<div />");

        var textBox = $("<input />").attr("type", "textbox").attr("name", "answers");
        textBox.val(value);
        div.append(textBox);

        var button = $("<input />").attr("type", "file").attr("files", "files");
        button.attr("onclick", "ChooseImage(this)");
        div.append(button);

        return div;
    }

    function AddTextBox() {
        var div = GetDynamicTextBox("");
        $("#TextBoxContainer").append(div);
    }

     function ChooseImage(button) {
         $(button).
     }

    $(function () {
        var values = eval('@Html.Raw(ViewBag.Values)');
        if (values != null) {
            $("#TextBoxContainer").html("");
            $(values).each(function () {
                $("#TextBoxContainer").append(GetDynamicTextBox(this));
            });
        }
    });
    </script>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="ذخیره" class="btn btn-success" />
        </div>
    </div>
</div>
}

my controller:

public ActionResult AddNewQuestion(Models.BestDestinationAddNewQuestionViewModel que, HttpPostedFileBase[] files, string[] answers)
{
   // my code
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Your not giving your file input a `name` attribute –  Aug 12 '17 at 09:33
  • However there are numerous other issues why this can fail - you cannot get client side validation, you cannot return the view if server side validation fails etc. Refer [this answer](https://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) for some options, and [this one](http://stackoverflow.com/questions/40539321/partial-view-passing-a-collection-using-the-html-begincollectionitem-helper/40541892#40541892) for a more detailed example using `BeginCollectionItem()` –  Aug 12 '17 at 09:44

0 Answers0