【问题标题】:ASP.NET MVC Text Fields not sent to model when form contains Files and Text Fields当表单包含文件和文本字段时,ASP.NET MVC 文本字段未发送到模型
【发布时间】:2020-07-12 01:50:39
【问题描述】:

我开发了一个表单,其中表单字段包含type=text 字段和type=file 字段。

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

    <div class="container">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="row justify-content-center align-items-center">
            <div class="card border-info col-sm-6">
                <div class="card-body">
                    <div class="form-group">
                        @Html.LabelFor(model => model.UploadForm.Name, new { @class = "control-label" })
                        @Html.TextBoxFor(model => model.UploadForm.Name, "", htmlAttributes: new { @type = "text", @class = "form-control", aria_describedby = "nameHelp", placeholder = "Enter name" })
                        <small id="nameHelp" class="form-text text-muted">Name to be associated</small>
                        @Html.ValidationMessageFor(model => model.UploadForm.Name, "", new { @class = "text-danger" })
                    </div>
                    <div class="form-group">
                        @Html.LabelFor(model => model.UploadForm.files, htmlAttributes: new { @class = "control-label" })
                        @Html.TextBoxFor(model => model.UploadForm.files, "", htmlAttributes: new { @type = "file", @multiple = "multiple", @class = "form-control-file", aria_describedby = "filesHelp" })
                        
                        <small id="filesHelp" class="form-text text-muted">Files to be uploaded</small>
                        @Html.ValidationMessageFor(model => model.UploadForm.files, "", new { @class = "text-danger" })
                    </div>
                    <div class="row justify-content-end" style="margin-right:1px;">
                        <input type="submit" value="Upload" class="btn btn-primary w-25"/>
                    </div>
                </div>
            </div>
        </div>
        <hr />
    </div>
}

我的控制器如下所示

[HttpPost]
public ActionResult UploadFiles(HomeModel model)
{        
    if (ModelState.IsValid)
    {
        **// getting model.UploadForm.Name null here**
        **// getting model.UploadForm.files is populated and contains files**
    }
    
    return View("Index", model);
}

我错过了什么吗?

另外,如果有人能告诉我,我该如何修改控制器以仅获取相关数据,如下所示。我试过这样做,但一切都为空。

UploadFiles (string name, HttpPostedFile[] files) 
{
        // both args are coming null
}

编辑:

这是我要求的模型:

public class HomeModel
{
    public UploadForm UploadForm { get; set; }
    public List<UploadedItem> UploadedItems { get; set; }
}

这就是 UploadForm 的样子

public class UploadForm
{
    [Display(Name = "Name")]
    [Required(ErrorMessage = "Please enter name.")]
    public string Name;

    [Required(ErrorMessage = "Please select file(s).")]
    [Display(Name = "Files")]
    public HttpPostedFileBase[] files { get; set; }
}

【问题讨论】:

  • 向我们展示您的模型。
  • @MrLu 更新了有问题的模型
  • 修改控制器获取相关数据是什么意思?
  • 目前,UploadFiles 操作正在获取整个模型 (HomeModel)。我想修改操作以仅获取相关数据(UploadedFilesName
  • @MrLu 谢谢。是的,我可以让它工作,所以将你的答案标记为答案。

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


【解决方案1】:

在您的 UploadForm 类中,Name is missing get set

public class UploadForm
{
    [Display(Name = "Name")]
    [Required(ErrorMessage = "Please enter name.")]
    public string Name { get; set; };

    [Required(ErrorMessage = "Please select file(s).")]
    [Display(Name = "Files")]
    public HttpPostedFileBase[] files { get; set; }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多