ASP.NET MVC loading - httpPostedFileBase is null

EDIT:

The photo has a null value in the post-controller method of the controller, despite the fact that I downloaded it.

My model class:

class ProductVM{
    public string Name { get; set;}
    public string Color {get; set;}
    public HttpPostedFileBase Photo1 { get; set; }
    }

Here's how I implemented the view using Razor:

    @model Project.Models.ProductVM

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


           @Html.AntiForgeryToken()
            @Html.ValidationSummary(true, "", new {@class = "text-danger"})

            @Html.EditorFor(model => model.Name, new {htmlAttributes = new {@class"form-control"}})
            @Html.ValidationMessageFor(model => model.Name)

        // other fields editor and dropdown ...


            <div class="col-xs-offset-2 col-xs-8 add-item-rectangle"><input type="file" name="@Model.Photo1" id="file"/></div>
            <div class="col-xs-10 add-item-rectangle"></div>

       <input type="submit" class="btn btn-block add-item-button-text" value="Send"/>

Postcontroller Method:

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult AddItem(ProductVM product)
        {            
             //**heere when debuging Photo1 is null despite fact that i uploaded photo**

            if (!ModelState.IsValid)
            {
                //... my stuffs
            }

            //..
           return RedirectToAction("Index", "Home");
        }
0
source share
2 answers

First of all, you cannot send messages directly to the byte array. As a result, you will need a presentation model to represent the product being created / modified. In your view model, your file upload properties should be printed as HttpPostedFileBase:

public HttpPostedFileBase Image1Upload { get; set; }
public HttpPostedFileBase Image2Upload { get; set; }

Your mail action will take your view model as a parameter:

[HttpPost]
public ActionResult CreateProduct(ProductViewModel model)

. :

if (model.Image1Upload != null && model.Image1Upload.ContentLength > 0)
{
    using (var ms = new MemoryStream())
    {
        model.Image1Upload.InputStream.CopyTo(ms);
        product.Image1 = ms.ToArray();
    }
}

. , , , , .

, .

UPDATE

:

<div class="col-xs-offset-2 col-xs-8 add-item-rectangle">
    <input type="file" name="@Model.Photo1" id="file"/>
</div>

-, @Model.Photo1 Model.Photo1, . Razor ToString , name, name="System.Web.HttpPostedFileBase". . :

<input type="file" name="@Html.NameFor(m => m.Photo1)" id="file" />

, , :

@Html.TextBoxFor(m => m.Photo1, new { type = "file" })
+2

:

  <div class="col-xs-offset-2 col-xs-8 add-item-rectangle">
       @Html.TextBoxFor(m => m.ItemFormDto.Photo1, new { type = "file" })
   </div>

, ? ( javascript - ?). , :

, : 1) acrea, - ( " ", 2) .

0

All Articles