2012-06-01 3 views
13

내 컨트롤러에서 비디오에 대한 세부 정보를 채우고 실제로 업로드 할 수 있기를 원했기 때문에 Video 클래스는 실제 비디오가 필요하지 않습니다. 다른 웹 서비스로 전달됩니다. ASP MVC 파일 업로드 HttpPostedFileBase가 null입니다.

public class VideoUploadModel 
    { 
     public HttpPostedFileBase vid { get; set; } 
     public Video videoModel { get; set; } 
    } 

    // 
    // POST: /Video/Create 
    [HttpPost] 
    public ActionResult Create(VideoUploadModel VM) 
    { 
     if (ModelState.IsValid) 
     { 
      db.Videos.AddObject(VM.videoModel); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     ViewBag.UserId = new SelectList(db.DBUsers, "Id", "FName", VM.videoModel.UserId); 
     return View(VM); 
    } 

내보기에서 나는 양식을 제출하면 VM의 videoModel 부분이 작성하지만 실제 파일을 VID됩니다

@model LifeHighlightsShoeLace.Controllers.VideoController.VideoUploadModel 
@{ 
    ViewBag.Title = "Create"; 
} 

<h2>Create</h2> 

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> 
@using (Html.BeginForm("Create", "Video", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
@Html.ValidationSummary(true) 
<fieldset> 
    <legend>Video</legend> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.videoModel.KalturaID) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.videoModel.KalturaID) 
     @Html.ValidationMessageFor(model => model.videoModel.KalturaID) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.videoModel.Size) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.videoModel.Size) 
     @Html.ValidationMessageFor(model => model.videoModel.Size) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.videoModel.Date) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.videoModel.Date) 
     @Html.ValidationMessageFor(model => model.videoModel.Date) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.videoModel.UploadedBy) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.videoModel.UploadedBy) 
     @Html.ValidationMessageFor(model => model.videoModel.UploadedBy) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.videoModel.UserId, "User") 
    </div> 

    <div class="editor-field"> 
     @Html.DropDownList("UserId", String.Empty) 
     @Html.ValidationMessageFor(model => model.videoModel.UserId) 
    </div> 
    <div class="editor-field"> 
    <input name="model.vid" type="file" /> 
    </div> 
    <p> 
     <input type="submit" value="Create" /> 
    </p> 
</fieldset> 

}가 null의이 . 아이디어가 있으십니까?

답변

19

업데이트

이 변경 web.config 파일의 최대 파일 길이를 설정 OP의 의견에 따라 "?"(물론 그냥 "장해를"당신의 "이름"으로 변경) 당신은 모델에 파일을 추가 할 수 없습니다 64메가바이트

<configuration> 
    <system.web> 
    <httpRuntime maxRequestLength="?" /> 
    </system.web> 
</configuration> 

65536은 예를 들어 당신이 최대로 원하는 파일 크기에, 그것의 자신의 분야에서 모델의 일부가 될 것입니다

<input name="videoUpload" type="file" /> 

귀하의 행동이 올바르지 않습니다.그것은 자신의 매개 변수의의 파일을 받아 들일 필요가있다 (또는 매개 변수 유형으로 여러 사용 IEnumerable<HttpPostedFileBase> 경우)

[HttpPost] 
public ActionResult Create(VideoUploadModel VM, HttpPostedFileBase videoUpload) 
{ 
    if (ModelState.IsValid) 
    { 
     if(videoUpload != null) { // save the file 
      var serverPath = server.MapPath("~/files/" + newName); 
      videoUpload.SaveAs(serverPath); 
     } 

     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 

    ViewBag.UserId = new SelectList(db.DBUsers, "Id", "FName", VM.videoModel.UserId); 
    return View(VM); 
} 

여러 파일을 허용 한 경우 해당

[HttpPost] 
public ActionResult Create(VideoUploadModel VM, IEnumerable<HttpPostedFileBase> videoUpload) 
{ 
    if (ModelState.IsValid) 
    { 
     if(videoUpload != null) { // save the file 
      foreach(var file in videoUpload) { 
       var serverPath = server.MapPath("~/files/" + file.Name); 
       file.SaveAs(serverPath); 
      } 
     } 

     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 

    ViewBag.UserId = new SelectList(db.DBUsers, "Id", "FName", VM.videoModel.UserId); 
    return View(VM); 
} 
+0

감사합니다 (그리고 @BuildStarted). 이제 create IE를 클릭하면 "IE에서 웹 페이지를 표시 할 수 없습니다."라는 오류가 표시되고 chrome이 localhost에 대한 연결이 중단되었음을 알립니다. 파일을 포함시키지 않으면 문제없이 게시됩니다. –

+0

4mb보다 큰 파일을 선택했습니다. –

+0

예. 고마워요! –

0

변화

<input name="model.vid" type="file" /> 

페이지에, 그리고 뷰가 렌더링되는 경우, MVC는 고유 ID의 생성됩니다 다른 내용에 따라

@Html.TextBoxFor(model => model.vid, new {type="file"}) 

에, 나는 당신의 하드 코드 된 ID를 생각한다 양식 필드와 올바르게 연결되어 있지 않습니다.

+0

참으로 나는 그 전에 있었고, 난 그냥 다시 시도 :

@Html.Label("Most Up-to-Date CV") <input type="file" name="File"/> 

나는 해결책이 배치 생각합니다. VM.vid는 여전히 null입니다. –

2

모델 바인더가 너 같은 복잡한 모델을 바인딩 할 때 모델 바인더는 QueryString, FormRouteData 만 보았 기 때문에 바인딩하지 않는 이유가 있습니다. 이 문제를 해결하는 방법은 액션 메소드에 다른 매개 변수를 지정하는 것입니다.

[HttpPost] 
public ActionResult Create(VideoUploadModel VM, HttpPostedFileBase vid) 
{ 
    //add your vid to the model or whatever you want to do with it :) 

    if (ModelState.IsValid) 
    { 
     db.Videos.AddObject(VM.videoModel); 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 

    ViewBag.UserId = new SelectList(db.DBUsers, "Id", "FName", VM.videoModel.UserId); 
    return View(VM); 
} 
+0

여기에서 당신을보고있다! :-) –

2

수 있도록해야 선택하기 나를 위해 작동 :

public class CreateVeiwModel 
    { 
     public Speaker Speaker { get; set; } 
     public Guid Guid { get; set; } 
     public HttpPostedFileBase File { get; set; } 
    } 

컨트롤러 :

[HttpPost] 
     public ActionResult Create(CreateVeiwModel model) 
     { 
      if (ModelState.IsValid) 
      try 
      { 
       Repository.AddSpeaker(model.Speaker); 
      ... 
     } 

보기 : Model.File ~ <input name="File"/>