Using AjaxUploader with MVC 3 Razor

Last post 01-23-2011, 11:00 PM by atwoodkevin. 3 replies.
Sort Posts: Previous Next
  •  12-27-2010, 2:38 PM 65532

    Using AjaxUploader with MVC 3 Razor

    Just thought I would post this for others that might be struggling with using AjaxUploader with MVC3 Razor syntax.  The problem I have been struggling with is that Razor automatically escapes Html code which is a good security practice.  The problem with AjaxUploader is the Uploader's Render() method which renders the html for injection into the view.  Since it renders Html, Razor's automatic scripting causes problems with inserting the html into your view. 
     
    Fixing this is a simple thing:  In your controller simply wrap the rendering call in a new HtmlString ie:
     
       [ChildActionOnly]
       [Authorize]
       public PartialViewResult UploadFiles()
       {
          using (CuteWebUI.MvcUploader uploader = new CuteWebUI.MvcUploader(System.Web.HttpContext.Current))
          {
    uploader.UploadUrl = Response.ApplyAppPathModifier("~/UploadHandler.ashx");
    //the data of the uploader will render as <input type='hidden' name='myuploader'> 
    uploader.Name = "myuploader";
    uploader.AllowedFileExtensions = "*.jpg,*.gif,*.png,*.bmp,*.zip,*.rar";
    //allow select multiple files
    uploader.MultipleFilesUpload = true;
    //tell uploader attach a button
    uploader.InsertButtonID = "uploadbutton";
    //prepair html code for the view
    ViewData["uploaderhtml"] = new HtmlString( uploader.Render());
          }
          return PartialView();
       }
    then in your view
    @using (Html.BeginForm())
    {
        <button id="uploadbutton" onclick="return false;">Select multiple files to upload</button>
        <!-- input type='hidden' id='myuploader' name='myuploader' -->
    <script type="text/javascript">
        function CuteWebUI_AjaxUploader_OnPostback() {
            //submit the form after the file have been uploaded:
            document.forms[0].submit();
        }
    </script>
        @ViewData["uploaderhtml"]
        
        }
           
      
        <!-- After posting the myuploader to server -->
        @if (@ViewData["UploadedMessage"] != null)
        { 

      @ViewData["UploadedMessage"]
            }
        


        

     
     
  •  12-27-2010, 2:41 PM 65533 in reply to 65532

    Re: Using AjaxUploader with MVC 3 Razor

    That above essentially gets the stuff rendered.  I will post any other fixes to issues with Razor as I find them.
  •  12-27-2010, 11:15 PM 65538 in reply to 65533

    Re: Using AjaxUploader with MVC 3 Razor

    Just an update on my progress. 
     
    I was able to get AjaxUploader to work completely with the MVC3 RC2 Razor ViewEngine.  The only really specific issue I had with regards to MVC3 Razor was as mentioned above.  Although they did introduce a breaking change by introducing "ViewBag" as an replacement for ViewModel (in controllers) and View.xxx (in views), which is not really an AjaxUploader issure, but does break the samples if you forget about it.
     
    Otherwise it runs smoothly modifying the sample3 sample for using Ajax.  I do all my processing using Ajax from my controller returning a JsonResult as noted in the sample.  Just have not found a way to completely get rid of the UploadHandler.ashx file yet.  Would like to do so.
  •  01-23-2011, 11:00 PM 65840 in reply to 65538

    Re: Using AjaxUploader with MVC 3 Razor

    How did you get the UploadHandler to function??
     
    Keeping getting:
     
    : The type or namespace name 'MvcHandler' does not exist in the namespace 'CuteWebUI' (are you missing an assembly reference?) 
View as RSS news feed in XML