2013-03-13 4 views
2

사용자가 데이터베이스에서 동적으로 생성되는 여러 페이지 양식을 작성할 수있는 사이트를 구축하고 있습니다. JQuery를 사용하여 컨트롤러에 데이터를 게시하고 양식의 다음 페이지를 반환합니다. 그것은 파일을 제외한 모든 것을 잘 작동합니다.MVC 4 동적 양식 파일 업로드

문제는 파일을 내 컨트롤러에 게시하는 것이므로 this post의 HtmlHelpers를 사용하여 파일 필드에 대한 html을 생성합니다.

내 모델 :

public class QuestionPage 
{ 
    public const string Next = "next"; 
    public const string Prev = "prev"; 
    public const string Save = "save"; 

    public int currentID { get; set; } 
    public bool advanced { get; set; } 
    public QuestionPageItem[] questions { get; set; } 
    public int pagenumber { get; set; } 
    public int? next { get; set; } 
    public int? prev { get; set; } 

    public string submitaction { get; set; } 
    public int? gotoID { get; set; } 

    public Dictionary<Int32, QuestionPageTitle> titles { get; set; } 
} 

public class QuestionPageItem 
{ 
    public int id { get; set; } 
    public string question { get; set; } 
    public string description { get; set; } 
    public bool required { get; set; } 
    public QuestionType type { get; set; } 
    public object answer { get; set; } 
    public int? enableID { get; set; } 
    public bool initHidden { get; set; } 
    public QuestionOptionIndexModel[] options { get; set; } 
} 

내 정적보기 :

using (Html.BeginForm("Form", "QuestionPage", new { id = Model }, FormMethod.Post, new { id = "frm" + Model, name = Model, enctype = "multipart/form-data" })) 
{ 
    <div id="formcontainer"> 
     @Html.Partial("_FormPartial", Model) 
    </div> 
} 

간략화의 jQuery AJAX로 대체 얻는다 내 부분 뷰 (_FormPartial가) :

@model DPDF.Models.QuestionPage 
    Some hidden fields... 
    @for (int i = 0; i < Model.questions.Length; i++) 
    { 
     var question = Model.questions[i]; 
     Some hidden fields... 
     <tr class="@(question.initHidden ? "hidden" : "")" id="@(question.id)" > 
      show textbox or textarea or radio etc depending on question type, for instance 
      @Html.TextBox("questions[" + i + "].answer", (question.answer == null ? string.Empty : question.answer.ToString())) 
      in case of file field 
      @Html.FileBox("questions[" + i + "].answer") 
     </tr> 
    } 

데이터 가져 질문 객체의 대답 필드에 바인딩됩니다. 내 컨트롤러 액션 : 텍스트 상자와 같은

[AllowAnonymous] 
    public ActionResult Form(QuestionPage model) 
    { 

     if (!ModelState.IsValid) 
      return View(model); 

     // this is to make some hidden fields get the correct new values 
     ModelState.Clear(); 

     // do stuff to determine what page to show next... 
     // save data, extract value from model.answer object depending on question type 
     // make new model and return it 
     if (Request.IsAjaxRequest()) 
      return PartialView("_FormPartial", model); 
     return View(model); 
    } 

일반 필드는 문자열 [] { "테스트"} 같은 것을 반환은, 파일 필드는 null를 돌려줍니다.

편집 : 아마 자바 스크립트에 문제가 있습니까?

var $form = $(form); 
    var $container = $('#formcontainer'); 
    $container.hide('slide', { direction: direction }, 500, function() { 
     $.ajax({ 
      url: $form.attr('action'), 
      type: $form.attr('method'), 
      data: $form.serialize(), 
      success: function (data, textStatus, jqXHR) { 
       $container.html(data); 
       updateOverzicht(); 
       $container.show('slide', { direction: (direction == 'left') ? 'right' : 'left' }, 500); 
      } 
     }); 
    }); 

답변

1

변경 QuestionPageItem 클래스 내부 answer의 유형이 HttpPostedFiledBase하지object이 될 수 :

public HttpPostedFileBase answer { get; set; } 

실제로 : 그의 진짜 문제는이 서버에 게시물 코드 JQuery에서 Ajax 요청의 일부로 양식을 제출하려고했기 때문에.

+0

체크 상자 목록에서 텍스트 상자 및 문자열 배열에서 문자열을 검색 할 수 있습니까? – Blight

+0

@Blight 아니요, 각 필드에'@Html.HiddenFor (m => Model.questions [i]. FieldName)'가 있어야합니다. :) – mattytommo

+0

게시 된 정보를 검색하는 방법을 보여줄 수 있습니까? 숨겨진 필드가있는 사용자가? – Blight

0

mattytommo에서 언급했듯이 파일 업로드는 이런 식으로는 불가능합니다.

게시하지 않고 페이지에있는 동안 파일을 업로드하기 위해 JQuery 플러그인을 사용한다고 생각합니다.

나는 그의 도움을 위해 mattytommo에게 감사한다.

의견에 문제가 있기 때문에 답을 올바르게 표시해야할지 확실하지 않습니다.

+0

내 대답을 확인, 그 문제를 포함하도록 편집했습니다 :) – mattytommo