Skip to main content

Posts

In MVC3 Razor, how do I get the html of a rendered view inside an action?

I use a static method in a class I called  Utilities.Common  I pass views back to the client as properties of JSON objects constantly so I had a need to render them to a string. Here ya go: public static string RenderPartialViewToString ( Controller controller , string viewName , object model ) { controller . ViewData . Model = model ; using ( StringWriter sw = new StringWriter ()) { ViewEngineResult viewResult = ViewEngines . Engines . FindPartialView ( controller . ControllerContext , viewName ); ViewContext viewContext = new ViewContext ( controller . ControllerContext , viewResult . View , controller . ViewData , controller . TempData , sw ); viewResult . View . Render ( viewContext , sw ); return sw . ToString (); } } This will work for full views as well as partial views, just change  ViewEngines.Engines.FindPartialView  to  ViewEngines.Engines.FindView . FindView ne...

How to download .xslx File from server using EPPlus and MVC - stream Response

All you need to do is reset the stream position.  stream.Position = 0; You  shouldn't write directly to the Response , it's not the MVC way. It doesn't follow the correct MVC pipeline and it tightly couples your controller action code to the Response object. When you add a file name as the 3rd parameter in  File() , MVC automatically adds the correct  Content-Disposition  header... so you shouldn't need to add it manually. The short of it is, this is what you want: public ActionResult Index () { using ( ExcelPackage package = new ExcelPackage ()) { // I populate the worksheet here. I'm 90% sure this is fine // because the stream file size changes based on what I pass to it. var stream = new MemoryStream (); package . SaveAs ( stream ); string fileName = "myfilename.xlsx" ; string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet...

SQL SERVER – Query to Find First and Last Day of Current Month

Following query will run respective to today’s date. It will return Last Day of Previous Month, First Day of Current Month, Today, Last Day of Previous Month and First Day of Next Month respective to current month. DECLARE  @mydate  DATETIME SELECT  @mydate  =  GETDATE () SELECT  CONVERT ( VARCHAR ( 25 ), DATEADD ( dd ,-( DAY ( @mydate )), @mydate ), 101 ) , 'Last Day of Previous Month' UNION SELECT  CONVERT ( VARCHAR ( 25 ), DATEADD ( dd ,-( DAY ( @mydate )- 1 ), @mydate ), 101 ) AS  Date_Value , 'First Day of Current Month'  AS  Date_Type UNION SELECT  CONVERT ( VARCHAR ( 25 ), @mydate , 101 )  AS  Date_Value ,  'Today'  AS Date_Type UNION SELECT  CONVERT ( VARCHAR ( 25 ), DATEADD ( dd ,-( DAY ( DATEADD ( mm , 1 , @mydate ))), DATEADD ( mm , 1 , @mydate )), 101 ) , 'Last Day of Current Month' UNION SELECT  CONVERT ( VARCHAR (...

Using try catch in SQL Server stored procedures

Overview A great new option that was added in SQL Server 2005 was the ability to use the Try..Catch paradigm that exists in other development languages.  Doing error handling in SQL Server has not always been the easiest thing, so this option definitely makes it much easier to code for and handle errors. Explanation If you are not familiar with the Try...Catch paradigm it is basically two blocks of code with your stored procedures that lets you execute some code, this is the Try section and if there are errors they are handled in the Catch section.  Let's take a look at an example of how this can be done.  As you can see we are using a basic SELECT statement that is contained within the TRY section, but for some reason if this fails it will run the code in the CATCH section and return the error information. CREATE PROCEDURE uspTryCatchTest AS BEGIN TRY     SELECT 1/0 END TRY BEGIN CATCH     SELECT ERROR_NUMBER() AS ErrorNu...

BCrypt.net - Strong Password Hashing for .NET and Mono

Using raw hash functions to authenticate passwords is as naive as using unsalted hash functions. Don’t. Thomas Ptacek BCrypt.net is an implementation of OpenBSD's Blowfish-based password hashing code, described in " A Future-Adaptable Password Scheme " by  Niels Provos  and  David Mazières . It is a direct port of  jBCrypt  by  Damien Miller , and is thus released under the same BSD-style license. The code is fully managed and should work with any little-endian CLI implementation -- it has been tested with Microsoft .NET and Mono. Why BCrypt? Most popular password storage schemes are based on fast hashing algorithms such as MD5 and SHA-1. BCrypt is a computationally expensive adaptive hashing scheme which utilizes the Blowfish block cipher. It is ideally suited for password storage, as its slow initialization time severely limits the effectiveness of brute force password cracking attempts. How much overhead it adds is configurable (that's the...

Cast Object to Generic List

Lots of trial and error gave me this on SL 5 but it should also work on a regular C#. You also need to add LINQ to your using list for the last half to work. List <object> myAnythingList = ( value as IEnumerable <object> ). Cast <object> (). ToList () Ref :  http://stackoverflow.com/questions/2837063/cast-object-to-generic-list