处理在ASP.NET MVC中动态添加的文件上传

时间:2018-09-30 16:26:23

标签: c# asp.net-mvc file-upload

晚安,亲爱的朋友,

我坚持在ASP.NET MVC的表单内动态添加文件上传。 我使用动态添加的控件(在表中插入行)创建表单。其中之一是文件上传。通过此jQuery函数添加控件:

$('#addRow').on('click', function () {
    var counter = $('.itemRow').length;
    $('#itemstable').append('<tr class="itemRow"> <td><select required name="ItemsList[' + counter + '].ProgramNameShort">@foreach (SelectListItem item in ViewBag.VBprograms) { <option value="@item.Value">@item.Text</option>} </select></td>' +
        '<td><input type="text" name="ItemsList[' + counter + '].ItemName" /></td>' +
        '<td><input type="text" name="ItemsList[' + counter + '].ItemProducer" /></td>' +
        '<td><input type="text" name="ItemsList[' + counter + '].ItemQty" /></td>' +
        '<td><input type="text" name="ItemsList[' + counter + '].Additional" /></td>' +
        '<td><input type="text" name="ItemsList[' + counter + '].Comments" /></td>' + 
        '<td><input type="file" name="ItemsList[' + counter + '].Image" /></td>' +
        '</tr>');

    counter++;
});

我的控制器收到ViewModel:

[HttpPost]
public ActionResult Create(CallViewModel callViewModel)
具有图像为[] byte属性的

ItemsList是CallViewModel类内的类。后来,对象被保存在数据库中(DB First方法)。据我了解,图像可以作为HttpPostedFileBase对象上传(对于多个文件上传,则为IEnumerable)。但是,如何处理此类列表中上传的文件? 我无法显示所有代码,因为它是多步骤多选项卡形式,表格只是其中一个选项卡的一部分。如果需要演示代码的任何部分,我可以更新当前问题。

非常感谢。

UPD 这是CallViewModel类的相关部分:

        public class CallViewModel
            {
    ... //properties used in other tabs

         public List<ItemClass> ItemsList { get { return _items; } }

         private List<ItemClass> _items = new List<ItemClass>();
public class ItemClass
        {  //Item

            public Guid ItemID { get; set; }

            [Required, Display(Name = "Наименование")]
            public string ItemName { get; set; }

            [Required, Display(Name = "Производитель")]
            public string ItemProducer { get; set; }

            [Required, Display(Name = "Количество")]
            public int ItemQty { get; set; }

            [Display(Name = "Комментарии")]
            public string Comments { get; set; }

            [Required, Display(Name = "Изображение")]
            public HttpPostedFileBase Image { get; set; }

            [Display(Name = "Дополнительно предоставляемые объекты")]
            public string Additional { get; set; }
            //Program
            //[Required, Display(Name = "Полное название")]
            //public string ProgramNameFull { get; set; }

            [Required, Display(Name = "Краткое название")]
            public string ProgramNameShort { get; set; }

            List<Program> Programs { get; set; }
}
}

2 个答案:

答案 0 :(得分:0)

    $('#addRow').on('click', function () {
        var counter = $('.itemRow').length;
        $('#itemstable').append('<tr class="itemRow"> <td><select required name="ItemsList[' + counter + '].ProgramNameShort">@foreach (SelectListItem item in ViewBag.VBprograms) { <option value="@item.Value">@item.Text</option>} </select></td>' +
            '<td><input type="text" name="ItemsList[' + counter + '].ItemName" /></td>' +
            '<td><input type="text" name="ItemsList[' + counter + '].ItemProducer" /></td>' +
            '<td><input type="text" name="ItemsList[' + counter + '].ItemQty" /></td>' +
            '<td><input type="text" name="ItemsList[' + counter + '].Additional" /></td>' +
            '<td><input type="text" name="ItemsList[' + counter + '].Comments" /></td>' + 
            '<td><input type="file" id="FileUpload ' + counter  + '"   /></td>' +
            '</tr>');

        counter++;
    });
--In controller do -- lengthofRow means no of row into ItemsList collection for(Int i=1; i<lengthofRow ;i++ ) { // from below line you can access you file upload by user . Request.Files["FileUpload" + i ]; }

答案 1 :(得分:0)

    <!--  Please define set property of Item List into Model -->
    <!-- begin snippet:hide: false console: true babel: false   -->
    <!-- language: lang-html -->    


 $('#addRow').on('click', function () {
        var counter = $('.itemRow').length;
        $('#itemstable').append('<tr class="itemRow"> <td><select required name="ItemsList[' + counter + '].ProgramNameShort">@foreach (SelectListItem item in ViewBag.VBprograms) { <option value="@item.Value">@item.Text</option>} </select></td>' +
            '<td><input type="text" name="ItemsList[' + counter + '].ItemName" /></td>' +
            '<td><input type="text" name="ItemsList[' + counter + '].ItemProducer" /></td>' +
            '<td><input type="text" name="ItemsList[' + counter + '].ItemQty" /></td>' +
            '<td><input type="text" name="ItemsList[' + counter + '].Additional" /></td>' +
            '<td><input type="text" name="ItemsList[' + counter + '].Comments" /></td>' + 
            '<td><input type="file" id="FileUpload ' + counter  + '"   /></td>' +
            '</tr>');

        counter++;
    });

    <!-- end snippet -->



    <!-- Please attach debugger and check you will all file into Request.File  -->
    <!--[HttpPost]  -->
    <!--public ActionResult Create(CallViewModel callViewModel)  -->
    <!--{  -->
        <!--   Int32 cnt=0;  -->
        <!--   cnt = CallViewModel.ItemsList.Count(); -->
         <!-- for(Int i=1; i<=cnt ;i++ )-->
        <!--  {  -->
            // from below line you can access you file upload by user .
         <!--    Request.Files["FileUpload" + i ];  -->
        <!--  }  -->
    <!--} -->