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); } } 
3
  • possible duplicate of Uploading image in ASP.NET MVC Commented Feb 3, 2015 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 :)) Commented Feb 3, 2015 at 19:43
  • @mattytommo that answers this question as well, you should have marked it duplicate..:) Commented Feb 4, 2015 at 4:35

1 Answer 1

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" })) 
Sign up to request clarification or add additional context in comments.

3 Comments

Beat me to it. This hasn't changed even with HTML 5.
Thank you, almost had it
@D-W No worries, glad I could help. I recognised it immediately as I've encountered the issue countless times :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.