Skip to content

Instantly share code, notes, and snippets.

View csharpforevermore's full-sized avatar
🏠
Working from home

Randle csharpforevermore

🏠
Working from home
View GitHub Profile
@csharpforevermore
csharpforevermore / StringToNameValueCollection.cs
Created November 15, 2013 18:13
Parse string into NameValueCollection
string nameValueString = "..."
NameValueCollection collection = HttpUtility.ParseQueryString(nameValueString);
@csharpforevermore
csharpforevermore / NameValueCollectionToString.cs
Created November 15, 2013 18:17
Parse a NameValueCollection to a string
var col = new NameValueCollection() { { "a", "b" }, { "1", "2" } }; // collection initializer
var values = col.Cast<string>().Select(e => col[e]); // b, 2
var str = String.Join(",", values); // "b,2"
@csharpforevermore
csharpforevermore / ListOfStringsToHtmlString.cs
Created November 19, 2013 12:16
Output a list of strings as an ordered list of HTML e.g. <ol> <li> ... </li><li>...</li></ol>
public static HtmlString OrderedList(IEnumerable<string> items)
{
var sb = new StringBuilder();
var orderedList = new TagBuilder("ol");
foreach (var item in items)
{
var listItem = new TagBuilder("li");
listItem.SetInnerText(item);
sb.AppendLine(listItem.ToString(TagRenderMode.Normal));
}
@csharpforevermore
csharpforevermore / GetPropertyAsCSV.cs
Created November 22, 2013 15:19
Get a property of a collection (e.g. Id property of MyClass) as a string CSV
class MyClass
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
//...
}
public static string GetPropertyIdAsList()
{
@csharpforevermore
csharpforevermore / ControllerBase.cs
Created November 25, 2013 21:11
MVC 4 Controller base class and example form class with standard Data Annotation values and custom validation attribute
public class ControllerBase : Controller
{
protected WebsiteContext LocalDatabase = new WebsiteContext();
protected override void Dispose(bool disposing)
{
LocalDatabase.Dispose();
base.Dispose(disposing);
}
}
@csharpforevermore
csharpforevermore / ParseToBoolean.cs
Created November 25, 2013 22:27
parse a web.config appsetting true / false to boolean.
bool isTest = Boolean.Parse(ConfigurationManager.AppSettings["appSettingValue"]);
@csharpforevermore
csharpforevermore / ConvertUpload.cs
Created November 27, 2013 16:14
In MVC 4 there is often the need to upload a file. When using model binding, you should use the upload field as type HttpPostedFileBase and for storing in the database you will need an array of bytes (i.e. byte[]). This is the way to convert from the former to the latter file format.
private byte[] ConvertHttpPostedFileToByteArray(HttpPostedFileBase rawUploadFile)
{
byte[] uploadImage;
using (var ms = new MemoryStream())
{
rawUploadFile.InputStream.CopyTo(ms);
uploadImage = ms.ToArray();
}
return uploadImage;
}
@csharpforevermore
csharpforevermore / ShowRedBorderOnTextBoxFor.css
Created November 28, 2013 15:02
Style the @Html.TextBoxFor helper when validation fails. This shows a red border
.field-validation-error
{
color: #ff0000;
}
.field-validation-valid
{
display: none;
}
.input-validation-error
{
public ActionResult Index()
{
string action = RouteData.GetRequiredString("action");
string controller = RouteData.GetRequiredString("controller");
string appDataPath = Server.MapPath("~/app_data");
string file = Path.Combine(appDataPath, controller + ".xml");
var xpath = "//item[@action='" + action + "']";
var item = XDocument.Load(file).XPathSelectElement(xpath);
if (item != null)
{
@csharpforevermore
csharpforevermore / FileUploadAspNet.cs
Last active December 30, 2015 04:09 — forked from Benrnz/FileUploadAspNet.cs
Uploads a file to the server and save it to disk. (INCOMPLETE)
public static bool UploadToServer(string pathToFile)
{
bool failure = false;
if (!FileUpload1.FileName.ToLowerInvariant().EndsWith(".xml")) {
failure = true;
}
StreamReader reader = new StreamReader(FileUpload1.FileContent);
string xmlContent = reader.ReadToEnd();
if (!xmlContent.TrimStart().StartsWith("<?xml")) {