1

Within my MVC 4 application when a user registers I'm looking to give them the option to select an image and upload that at the same time as registering - I was just looking at some guidance as to how to do this.

Here is my MVC Model for registering - I've added HttpPostedFileBase to hold the file being posted along with the username and password for new account being registered:

public class RegisterModel
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }

    public HttpPostedFileBase File { get; set; }

}

The registration View includes the following input html:

<input id="register-avatar"  type="file" name="file" />

However when I register, the value for the file is always null...

UPDATE 04-11-2013

I finally created a post which doesn't seem to be covered on how to include a profile image upload with the generic RegisterModel within the MVC 4 Template. You can find it here - MVC 4 Add Profile Image to RegisterModel

UPDATE

The problem as mentioned in the comment below is that I did not have the enctype set. For more information have a look here

Community
  • 1
  • 1
dotnethaggis
  • 1,000
  • 12
  • 26
  • 2
    Did you set the enctype of the form to `multipart/form-data`? – WannaCSharp Aug 31 '13 at 15:42
  • That's exactly the issue - I've updated my question to highlight that. – dotnethaggis Aug 31 '13 at 16:00
  • **[Please check here](http://stackoverflow.com/questions/18415206/dataannotation-regular-expression-always-returns-false-for-file-input/18415686#18415686)** – Imad Alazani Aug 31 '13 at 17:12
  • Is it possible to share the code of that register model and view, so users can add a profile picture? – Odrai Oct 08 '13 at 09:02
  • @Odrai You've probably worked it out already but I thought I would let you know that I created a post that quickly explains how to add an image to the RegisterModel included in MVC 4 - http://stackoverflow.com/questions/19776346/mvc-4-add-profile-image-to-registermodel/19776347#19776347 – dotnethaggis Nov 04 '13 at 20:20

2 Answers2

0

As far as I know, id is a stronger identity than name, so your id attribute should be "file" instead of "register-avatar".

The code inside the view should be:

<input type="file" id="File" name="File" />
Aviran Cohen
  • 5,581
  • 4
  • 48
  • 75
0

The form's encoding type was missing - multipart/form-data.

It's explained further here.

Community
  • 1
  • 1
dotnethaggis
  • 1,000
  • 12
  • 26