Input type file not showing ASP.NET

Viewed 336

I have this model:

public class PostAddRequest
{
    public string Title { get; set; }
    public string Description { get; set; }
    public string Content { get; set; }
    public int CategoryId { get; set; }
    public string FeaturedImagePath { get; set; }
    public HttpPostedFileBase FeaturedImage { get; set; }
    public List<string> GalleryImagesPath { get; set; }
    public HttpPostedFileBase[] GalleryImages { get; set; }
}

Controller:

    [HttpGet]
    public ActionResult Create()
    {
        return View(new PostAddRequest());
    }

View:

@model BusinessObjects.Requests.PostAddRequest

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>


@using (Html.BeginForm("Create", "Post", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{
    @Html.AntiForgeryToken()
    
<div class="form-horizontal">
    <h4>PostAddRequest</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Content, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Content, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Content, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.CategoryId, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.CategoryId, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.CategoryId, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.FeaturedImage, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.TextBoxFor(model => model.FeaturedImage, new { type = "file", htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.FeaturedImage, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.GalleryImages, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.TextBoxFor(model => model.GalleryImages, "", new { @type = "file", @multiple = "multiple" })
            @Html.ValidationMessageFor(model => model.GalleryImages, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

I'm getting this error: Compiler Error Message: CS1061: 'PostAddRequest' does not contain a definition for 'GalleryImages' and no accessible extension method 'GalleryImages' accepting a first argument of type 'PostAddRequest' could be found (are you missing a using directive or an assembly reference?)

I want to upload an image for FeaturedImage and multiple images for GalleryImages, but for some reason I'm getting the above error. Any idea on how to fix this?

1 Answers

you should use List<IFormFile> for multiple files.

public List<IFormFile> GalleryImagesPath { get; set; }
Related