2013-08-31 46 views
1

在我的MVC 4應用程序中,當用戶註冊時我正在給他們選擇一個圖像並在註冊的同時上傳它的選項 - 我只是看一些指導這個怎麼做。MVC 4 RegisterModel include Profile Image

這裏是我的MVC模型註冊 - 我已經添加HttpPostedFileBase用來存放文件與新帳戶的用戶名和密碼一起被公佈被註冊:

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; } 

} 

註冊查看包括以下輸入HTML :

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

然而,當我註冊,該文件的值總是空...

UPDATE 2013年4月11日

我終於創建了一篇文章,似乎並未涉及如何使用MVC 4模板中的通用RegisterModel包含配置文件圖像上傳。你可以在這裏找到它 - MVC 4 Add Profile Image to RegisterModel

UPDATE

如在下面的評論中提到的問題是,我沒有足夠的加密類型集。欲瞭解更多信息,看看here

+2

您是否將窗體的enctype設置爲'multipart/form-data'? – WannaCSharp

+0

正是這個問題 - 我已經更新了我的問題來強調這一點。 – dotnethaggis

+0

** [請點擊這裏](http://stackoverflow.com/questions/18415206/dataannotation-regular-expression-always-returns-false-for-file-input/18415686#18415686)** –

回答

0

表單的編碼類型缺失 - multipart/form-data

這是進一步解釋here

0

據我所知,idname更強的身份,所以你id屬性應該是「文件」,而不是「註冊具象」。

視圖中的代碼應該是:

<input type="file" id="File" name="File" /> 
+0

感謝您的信息,但這不是問題。這是上面提到的enctype。 – dotnethaggis