1

Ok, I keep getting null returned on my photo, What am I missing?

I've trid this Uploading/Displaying Images in MVC 4

But Im obviously looking to include the image as part of model. Model

public class Client
    {
        public string Name{ get; set; }
        public string ClientId { get; set; }
        public HttpPostedFileBase Photo{ get; set; }
}

Page

@using (Html.BeginForm()) { 
    @Html.ValidationSummary(true)
    <div class="form-horizontal" role="form">
                        <div class="form-group">
                            <label for="@Model.Name" class="col-sm-2 control-label">Name:</label>
                            <div class="col-sm-10">
                                <input name="ClientId" type="hidden" value="@Model.ClientId" id="ClientId" data-val="true" data-val-required=" required">
                                <input type="text" id="Name" value="" class="form-control" tabindex="1" placeholder="name" name="Name" required/>
                                @Html.ValidationMessageFor(model => model.Name)
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="@Model.Name" class="col-sm-2 control-label">Image:</label>
                            <div class="col-sm-10">
                                @Html.TextBoxFor(m => m.Photo, new { type = "file" })
                            </div>
                        </div>
}

Controller

[HttpPost]
        [Authorize]
        public ActionResult New(ViewModels.Client client)
        {
if (client.Photo!= null)
            {
                var photo= new byte[client.Photo.ContentLength];
                client.Photo.InputStream.Read(photo, 0, client.Photo.ContentLength);

            }
}
Community
  • 1
  • 1
D-W
  • 5,201
  • 14
  • 47
  • 74
  • possible duplicate of [Uploading image in ASP.NET MVC](http://stackoverflow.com/questions/10402094/uploading-image-in-asp-net-mvc) – Ehsan Sajjad Feb 03 '15 at 16:59
  • @EhsanSajjad Technically this is different, as it's the omission of the `enctype` that makes it different (I answered that other question aswell :)) – Mathew Thompson Feb 03 '15 at 19:43
  • @mattytommo that answers this question as well, you should have marked it duplicate..:) – Ehsan Sajjad Feb 04 '15 at 04:35

1 Answers1

3

You haven't set the enctype on the form (make sure you change Controller below to be the name of your controller):

@using (Html.BeginForm("New", "Controller", FormMethod.Post, 
    new { enctype = "multipart/form-data" }))
Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148