2015-10-21 61 views
1

我有一個表格和一個上傳(我使用PLUploader),並希望用戶填寫文本框和PLUploader中選擇圖像,當點擊提交按鈕,0​​我傳遞圖像和文本框值一個動作,我寫這個代碼,但總是我在文本框的值爲空,但獲取圖像的行動。傳遞文件和模型到一個動作asp.net

我覺得這個問題涉及到用form和PLuploader調用一個動作。

public ActionResult Insert(News news, HttpPostedFileBase file) 
{ 
    // I get null in new but get file in HttpPostedFileBase 
    int result = 0; 

    HttpPostedFileBase FileData = Request.Files[0]; 

    string fileName = null; 

    fileName = Path.GetFileName(FileData.FileName); 

    if (ModelState.IsValid) 
    { 
     //do some thing 
    } 
    else 
    { 
     return View(news); 
    } 
} 

@using (Html.BeginForm("Insert", "News", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
    <div class="col-xs-12"> 
     @Html.LabelFor(model => model.NewsTitle) 
     @Html.TextBoxFor(model => model.NewsTitle, new { @class = "form-control",@name="title" }) 
     @Html.ValidationMessageFor(model => model.NewsTitle) 
    </div> 

    <div class="col-xs-12"> 
     <div id="uploader" class="img-plc"> 
      <p>You browser doesn't have Flash, Silverlight, Gears, BrowserPlus or HTML5 support.</p> 
     </div> 
     <ul id="gallery"></ul> 
    </div> 
    <div class="col-xs-12"> 
     @Html.LabelFor(model => model.NewsText, new { @class = "text-right" }) 
     @Html.ValidationMessageFor(model => model.NewsText) 

     @Html.TextAreaFor(model => model.NewsText, new { @rows = "10", @cols = "80", @class = "text-editor", @name = "title" }) 
    </div> 

    <button type="submit">Submit</button> 
} 

var uploader = $("#uploader").pluploadQueue({ 
       // General settings 
       runtimes: 'html5,gears,flash,silverlight,browserplus,html4', 
       url: '@Url.Action("Insert", "News")', 
       max_file_size: '10mb', 
       chunk_size: '1mb', 
       unique_names: true, 
       multi_selection: false, 
       multiple_queues: false, 

       // Specify what files to browse for 
       filters: [ 
        { title: "Image files", extensions: "jpg,png" } 
       ], 

       // Flash settings 
       flash_swf_url: '/Scripts/Moxie.swf', 

       // Silverlight settings 
       silverlight_xap_url: '/Scripts/Moxie.xap' 

      }) 

    $('form').submit(function (e) { 
       var uploader = $('#uploader').pluploadQueue(); 

       // Files in queue upload them first 
       if (uploader.files.length > 0) { 
        // When all files are uploaded submit form 
        uploader.bind('StateChanged', function() { 
         if (uploader.files.length === (uploader.total.uploaded + uploader.total.failed)) { 
          $('form')[0].submit(); 
         } 
        }); 

        uploader.start(); 
       } else { 
        alert('You must queue at least one file.'); 
       } 

       return false; 
      }); 

我該如何解決這個問題?我想在這個行動中獲得新聞和檔案。

回答

0

創建一個視圖模型包含兩個屬性

public class NewsViewModel { 

    public News News { get; set; } 
    public HttpPostedFileBase File { get; set; } 

} 

public ActionResult Insert(NewsViewModel model) { 

    /* ... */ 

} 

當您創建視圖通過視圖模型到視圖中。確保你使用正確的名稱輸入字段,使其正確綁定:

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

我會假設你可能要告訴你的腳本文件輸入建議立即進行刪除有什麼名字。

相關問題