Posts

Showing posts with the label MVC 4

Azure: How do I create an MVC site to load files to BLOB storage?

I recently created a simple POC site to load files to blob storage containers in Azure. Here is how I achieved it. My code is based on the sample provided  here After creating a simple MVC web project, I added the following nuget packages: DropZone WindowsAzure.Storage Microsoft.WindowsAzure.ConfigurationManager The code is pretty simple: Here is Index.cshtml: @model List<string> <script src="~/Scripts/dropzone/dropzone.min.js"></script> <script src="~/Scripts/bootstrap.min.js"></script> <link href="~/Scripts/dropzone/dropzone.min.css" rel="stylesheet" /> <div class="jumbotron">     <h2>FILE UPLOAD</h2>     <form action="~/Home/Upload"           class="dropzone"           id="dropzoneJsForm"           style="background-color:#00BFFF"></form>         <button id="refresh" onc...

MVC 4: The required anti-forgery form field "__RequestVerificationToken" is not present

It seems the best things in life come in pairs: If you have a ValidateAntiForgeryToken annotation in your controller, you need to have a @Html.AntiForgeryToken() in your view.

MVC4: Why cant I resolve the WebSecurity class?

While trying to create a two step password reset page, I struggled to find the required reference for the WebSecurity class (to generate the ResetToken). The solution is to add a reference to C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v2.0\Assemblies\WebMatrix.WebData.dll

Visual Studio 2012: Error loading 'NuGet' when creating a new MVC project

I tried to create a new MVC 4 project in Visual Studio 2012 and was greeted with the following message: Error: this template attempted to load component assembly 'NuGet.VisualStudio.Interop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. For more information on this problem and how to enable this template, please see documentation on Customizing Project Templates. The fix is pretty simple - download and install the NuGet Package Manager from  http://visualstudiogallery.msdn.microsoft.com/27077b70-9dad-4c64-adcf-c7cf6bc9970c

MVC 4: How do I download a list as a CSV?

I added a simple link in my view @Html.ActionLink("Export Data", "ExportData") and a simple method in my controller to take care of the download public ActionResult ExportData() { return DownloadCSV(); } public FileContentResult DownloadCSV() { string csv = string.Concat(from x in db.MyData   select x.ID + ","   + x.Name + "\n"); return File(new System.Text.UTF8Encoding().GetBytes(csv), "text/csv",  string.Format("Export.{0}.csv", DateTime.Now.ToString("yyyy-MM-dd_HHmmss"))); }

MVC 4: How do I line-wrap my text in an editor?

The solution is to add an annotation to the field in the model class: [DataType(DataType.MultilineText)] Then, to display the item in the view, use Html.Raw. @Html.Raw("<pre>"+ Html.Encode(@Model.MyField) + "</pre>"

MVC 4: How do I generate a pdf file from a view?

I looked at several possible solutions (including itextsharp) but there were always issues rendering the CSS correctly. Fortunately, I found the following gem in codeplex:  http://pdfgenerator.codeplex.com/ I used the following code to generate the HTML from the view (sourced from here ):  public string RenderViewToString(Controller controller, string viewName, object viewData)         {             var renderedView = new StringBuilder();             using (var responseWriter = new StringWriter(renderedView))             {                 var fakeResponse = new HttpResponse(responseWriter);                 var fakeContext = new HttpContext(HttpContext.Current.Request, fakeResponse);                 var fakeControllerContext = new Controll...