2012-10-15 2 views
1

내 게시물에 관계없이 이미지는 항상 null이거나 개체 참조가 개체의 인스턴스로 설정되지 않습니다.MVC 4 HttpPostedFileBase always null

보기 :

<h2>Create</h2> 
@using (Html.BeginForm("Create", "Admin", FormMethod.Post, new{ enctype =   "multipart/form-data" })) 
{ 

@Html.ValidationSummary(true) 

@Html.LabelFor(post => post.Post.Blog) 
@Html.DropDownListFor(post => post.Post.BlogId, Model.BlogList) 
@Html.LabelFor(post => post.Post.Category) 
@Html.DropDownListFor(post => post.Post.CategoryId, Model.CategoryList) 

@Html.LabelFor(post => post.Post.Title) 
@Html.EditorFor(post => post.Post.Title) 
<br /> 


@Html.LabelFor(post => post.Post.Content) 
@Html.EditorFor(post => post.Post.Content) 
    <br /> 
<input type="file" name="uploadImage" value="Upload" /> 
<input type="submit" value="Create" /> 
} 

작업 방법 :

[HttpPost] 
    public ActionResult Create(Post post, HttpPostedFileBase image) 
    { 

     if (ModelState.IsValid) 
     { 
      var postBlog = _repository.Blogs.Single(b => b.BlogId == post.BlogId); 
      post.Created = DateTime.Now; 
      postBlog.Posts.Add(post); 



       PostImage newImage = new PostImage() 
             { 
              MimeType = image.ContentType, 
              ImageData = new byte[image.ContentLength], 
              PostId = post.PostId 
             }; 
       _repository.AddImage(newImage); 



      _repository.Save(); 
      return RedirectToAction("Index"); 

     } 

나는

여기

내 코드입니다 (첫 번째 EF 코드를 사용하여) 테이블에 매핑 게시물 클래스와 이미지 클래스가

답변

3

Doh! 문제는 내가 input 요소에 'value'를 설정했다는 것입니다. 올바르게 입력하기 위해서는 input type = "file"name = "Image"/>가 필요합니다.

관련 문제