2015-10-20 55 views
0

我有一个表格和一个上传(我使用PLUploader),并希望用户填写文本框和选择图像在PLUploader中,当点击提交按钮,我通过图像和textboxs价值的一个动作,我写这篇文章的代码,但alwaye我得到textboxs null值,但在行动中获得的图像,我觉得这个问题要拨打与形式和PLuploader的一个动作相关,传递上传文件和形式提交值的一个动作在asp.net mvc

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

你没有提交你的整个形式;而不是你捕获与jQuery的事件处理程序提交行动onSubmit

$('form').submit(function (e) { ... }); 

如果你想这样做,这样,你就必须序列化此jQuery代码块中的News模型以及和后它与文件上传一起。

我只是坚持HttpPostedFileBase在新闻模型,并使用正常形式提交虽然。

+0

感谢您的回放,你能告诉我示例代码吗? –