2013-05-07 28 views
0

我有2個問題:精細上傳的形式不提交模型數據到控制器

1)我已經把精細上傳的形式,現在我不知道爲什麼我沒有看到填充我的控制器中輸入字段的內容。我如何解決這個問題?

2.)如何在上傳過程觸發前做字段驗證?

我希望有人能幫助!

這裏是我的代碼:

型號:

public class UploadFileModel 
{ 
    [StringLength(100)] 
    [Display(Name = "* Title:")] 
    public string Title { get; set; } 

    [StringLength(300)] 
    [Display(Name = "Description:")] 
    public string Description { get; set; } 
} 

查看:

@model mowee.Models.UploadFileModel 

<link href="~/Scripts/fineuploader/fineuploader-3.5.0.css" rel="stylesheet" /> 

@using (Html.BeginForm("UploadFile", "Home", FormMethod.Post, new { id = "uploadForm", enctype = "multipart/form-data" })) 
{ 
    @Html.AntiForgeryToken() 
    @Html.ValidationSummary() 

    <fieldset> 
     <legend>Video Upload</legend> 
     <ol> 
      <li> 
       @Html.LabelFor(m => m.Title) 
       @Html.TextBoxFor(m => m.Title, new { @Class = "action add", title = "Enter your video/movie title here." }) 
      </li> 
      <li> 
       @Html.LabelFor(m => m.Description) 
       @Html.TextAreaFor(m => m.Description, new Dictionary<string, object> { { "rows", "3" } }) 
      </li> 
     </ol> 

     <div id="bootstrapped-fine-uploader"></div> 
     <script src="~/Scripts/fineuploader/fineuploader-3.5.0.js"></script> 
     <script> 
      function createUploader() { 
       var uploader = new qq.FineUploader({ 
        element: document.getElementById('bootstrapped-fine-uploader'), 

        multiple: false, 
        validation: { 
         sizeLimit: 2147483648 //2GB 
        }, 
        request: { 
         endpoint: '/Home/UploadFile' 
        }, 
        text: { 
         uploadButton: '<div><i class="icon-upload icon-white"></i>* Upload Video</div>' 
        }, 
        template: '<div class="qq-uploader span12">' + 
        '<pre class="qq-upload-drop-area span12"><span>{dragZoneText}</span></pre>' + 
        '<div id="btnUpload" class="qq-upload-button btn btn-success" style="width: 33%;">{uploadButtonText}</div>' + 
        '<span class="qq-drop-processing"><span>{dropProcessingText}</span><span class="qq-drop-processing-spinner"></span></span>' + 
        '<ul class="qq-upload-list" style="margin-top: 10px; text-align: center;"></ul>' + 
        '</div>', 
        classes: { 
         success: 'alert alert-success', 
         fail: 'alert alert-error', 
         dropActive: "cssClassToAddToDropZoneOnEnter" 
        } 
       }); 
      } 
      window.onload = createUploader; 
     </script> 
    </fieldset> 
} 

控制器:

[HttpPost] 
[AllowAnonymous] 
[AcceptVerbs(HttpVerbs.Post)] 
public JsonResult UploadFile(string qqfile, UploadFileModel myModel) 
{ 
    if (ModelState.IsValid) 
    { 
     try 
     { 
      HttpPostedFileBase uploadFile = null; 
      uploadFile = Request.Files[0]; 

      if (uploadFile != null && uploadFile.ContentLength > 0) 
      { 
       if (myModel.Title != null || myModel.Description != null) 
       { 
        **//but UploadFileModel is null. WHY??????? Anyone an idea what i did wrong??? 
        //write data to DB** 
       } 
      } 
      else 
      { 
       ModelState.AddModelError("", "Please choose a video/movie."); 
       return Json(new { success = false, message = "Please choose a video/movie." }, "application/json"); 
      } 
     } 
     catch (Exception ex) 
     { 
      ModelState.AddModelError("", "An error occured. Try again."); 
      mailUtils.SendBugMail(ex, this.HttpContext); 
      return Json(new { success = false, message = "An error occured during upload. Try again." }, "application/json"); 
     } 
     return Json(new { success = true, VideoLink = @ViewBag.VideoLink, VideoTranscoded = @ViewBag.Transcoded }, "application/json");//"text/html"); 
    } 
    return Json(new { success = false, message = "An error occured during upload. Try again." }, "application/json"); 
} 

回答

0

解決您的問題:

  1. 你應該真的看看在Fine Uploader server-side examples repositoryMVC4 example。在這個例子之後建立你的服務器端代碼的模型,你應該沒問題。我個人不是MVC4開發者,但我知道我已經鏈接到作品的例子。
  2. 您可以使用onValidateonValidateBatch回調來執行您自己的驗證任務。請查看callbacks documentation瞭解更多信息。
1

好的。我想到了。 params可以設置回調'onSubmit'與setParams這樣的:

   callbacks: { 
        onSubmit: function (id, fileName, responseJSON) { 
         uploader.setParams({ 
          idxTitle: $('#titleId').val(), 
          idxAuthor: $('#authorId').val(), 
          idxEmail: $('#emailId').val() 
         }); 
        }, 

它工作正常!

相關問題