Skip to main content

Posts

Showing posts from August, 2013

How to convert Dictionary to a comma separated csv string in c#?

the following function will take the list of dictionary items to a comma separated csv string. public string ConvertToCSV(List<Dictionary<string, string>> items) {     if (!items.Any()) return string.Empty;     StringBuilder writer = new StringBuilder();     // Generating Header.     List<string> headers = items[0].Keys.Select(x => x).OrderBy(x => x).ToList();     writer.AppendLine(string.Join(", ", headers.Select(h => h)));     // Generating content.     foreach (var item in items)     writer.AppendLine(string.Join(", ", headers.Select(h => item[h])));     return writer.ToString(); }  

MVC : How to add attribute, class, readonly, custom attribute to @Html helper classes in MVC like @Html.TextBoxFor

Here I am using the examples for @Html.TextBoxFor class. 1) Adding attributes @Html.TextBoxFor(model=>model.FirstName,new{ Width="200px" }) 2) Adding Custom attribute @Html.TextBoxFor(model=>model.FirstName,new{ @data_type="number" }) Note : data_type will be converted to data-type by Razor engine. 3) Readonly & Class @Html.TextBoxFor(model=>model.FirstName,new{ @readonly="readonly", @class="firstname" })

MVC: How to use enum type in drop down list using mvc

To use enum type in dropdown list follow the following format. @Html.DropDownListFor(model => model.EnumName, new SelectList(Enum.GetValues(typeof(Namespace.EnumName))))    //Enum namespace MyNameSpace.Util {    public enum Days    {      Sunday,      Monday,      Tuesday,      Wednesday,      Thursday,      Friday,      Saturday    } } //Model public EnumTestModel {    MyNameSpace.Util.Days SelectedDay {get;set;} } Razor syntax for displaying Days in DropDownList is // View @Html.DropDownListFor(model => model.SelectedDay , new SelectList(Enum.GetValues(typeof(MyNameSpace.Util. Days ))))