Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 动态表单上的MVC模型验证?_C#_Asp.net_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

C# 动态表单上的MVC模型验证?

C# 动态表单上的MVC模型验证?,c#,asp.net,asp.net-mvc,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,我有以下型号: public class FileModel { public int Id { get; set; } [Required(ErrorMessage = "Required")] [StringLength(100, ErrorMessage = "Max is 100, Min is 3", MinimumLength = 3)] public string Name { get; set; } public string Path

我有以下型号:

public class FileModel
{
    public int Id { get; set; }

    [Required(ErrorMessage = "Required")]
    [StringLength(100, ErrorMessage = "Max is 100, Min is 3", MinimumLength = 3)]
    public string Name { get; set; }

    public string Path { get; set; }

    [Required(ErrorMessage = "Required")]
    public string FileTypeId { get; set; }

    public DateTime RegistrationDate { get; set; }
}
以下是我的看法:

@using (Html.BeginForm("Index", "FileManagement", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <table class="content-table" style="min-width: 600px; border-spacing: 15px;">
        <tr>
            <td colspan="4" class="table-header">New File
                <div class="add-icon">+</div>
            </td>
        </tr>
        <tr>
            <td>Name: </td>
            <td>@Html.TextBoxFor(q => q.NewFile.Name, new { maxlength = "100", id = "NewFile_Name1", name = "NewFile.Name1" })
                <br />@Html.ValidationMessageFor(q => q.NewFile.Name)
            </td>
            <td>
                <input type="file" id="FileUploadField1" /></td>
            <td style="width: 16px; text-align: center;">&nbsp;</td>
        </tr>
        <tr>
            <td colspan="4" style="text-align: center;">
                <input type="submit" value="Submit" />
            </td>
        </tr>
    </table>
    <script type="text/javascript">

        $('.content-table .add-icon').click(function () {
            var lastFileField = $('.content-table input[type="file"]').last();
            var lastId = lastFileField.attr('id').replace(/\D*/g, '');
            lastId = parseInt(lastId) + 1;
            var newFields = '<tr>' +
                '<td>Name : </td>' +
                '<td><input data-val="true" data-val-length="Max chars is 100, Min chars is 3" data-val-length-max="100" data-val-length-min="3" data-val-required="Required" id="NewFile_Name' + lastId + '" name="NewFile.Name' + lastId + '" type="text" value="" /><br /><span class="field-validation-valid" data-valmsg-for="NewFile.Name' + lastId + '" data-valmsg-replace="true"></span></td>' +
                '<td><input type="file" id="FileUploadField' + lastId + '"/></td>' +
                '<td style="text-align:right;"><div class="delete-icon"></div></td>' +
                '</tr>';
            var lastTr = $(lastFileField).parents('tr:first')[0];
            $(lastTr).after(newFields);
        });

        $('.content-table .delete-icon').live('click', function () {
            $(this).parents('tr:first').remove();
        });
    </script>
}
@使用(Html.BeginForm(“Index”,“FileManagement”,FormMethod.Post,new{enctype=“multipart/formdata”}))
{
新文件
+
姓名:
@Html.TextBoxFor(q=>q.NewFile.Name,new{maxlength=“100”,id=“NewFile\u Name1”,Name=“NewFile.Name1”})

@Html.ValidationMessageFor(q=>q.NewFile.Name) $('.content table.add icon')。单击(函数(){ var lastFileField=$('.content表输入[type=“file”]')。last(); var lastId=lastFileField.attr('id')。替换(/\D*/g'); lastId=parseInt(lastId)+1; var newFields=''+ '姓名:'+ “
”+ '' + '' + ''; var lastTr=$(lastFileField).parents('tr:first')[0]; $(lastTr).after(newFields); }); $('.content table.delete icon').live('单击',函数(){ $(this.parents('tr:first').remove(); }); }
如您所见,我们有一个用于上载一个或多个文件的表单。因此,我为用户添加了一个+按钮,用户可以在表单中添加一个文件字段。

用户必须输入文件名并选择要上载的文件。但是MVC客户机验证器只验证添加了
Razor


的第一个输入 如何强制MVC验证程序验证客户端和服务器端的所有字段。

另一个问题是:
我们如何处理在MVC控制器上获取字段值的问题

谢谢

将帮助您了解默认模型绑定器将如何绑定列表和数组。只需为页面创建一个如下所示的ViewModel:

public class FileUploadViewModel
{
    public List<FileModel> lFileModels { get; set; }
}
编辑

用于MVC文件上载

编辑2

在再次阅读您的代码之后,我现在意识到验证问题是由于在加载库之后添加了不引人注目的验证。您必须重新解析验证器

$("form").data("validator", null);
$.validator.unobtrusive.parse($("form"));
绑定适用于服务器端验证和问题的第二部分

编辑3


为了添加字段,而不是直接在JS中进行,您应该使用Ajax加载表单的部分和文件字段。单击“添加”按钮时,它会请求文件字段的局部视图,其中的post数据中包含当前项目的列表。然后,局部视图返回带有额外字段的渲染视图。这只是一个想法。我还没试过,甚至没见过这个主意。我只是想和大家分享一下。

我的问题不仅仅是数据模型绑定,主要的问题是添加了javascriptI的输入字段的客户端验证。我用解决方案回答了这两个问题,但没有解释。对不起,我正在编辑我的帖子。再次阅读后,我意识到我实际上偏离了轨道。我所有的帖子只回答了你的子问题和服务器端验证。这不会影响客户端。Edit2是对原始帖子的回答。你应该在题目中使用恰当的术语。模型验证是服务器端验证。这就是我感到困惑的原因(但是是的,我也应该更加关注这篇文章本身)。
$("form").data("validator", null);
$.validator.unobtrusive.parse($("form"));