Posts

Donate

@Html.EnumDropDownListFor() Helper With Html Attributes In ASP.NET MVC

Hello, I did some research on how to use enums as model for DropDownListFor() for a current application and will format it with Bootstrap classes. For ASP.NET MVC 4, it does not have that kind of helper yet. Luckily, I found an article here Creating a DropDownList helper for enums which supports enums as data source. However, it does not have an argument where in you can pass a boostrap class such as form-control. To achieve the desired output, all you need to do is modify the helper and add another parameter for html attributes which will then be used by the DropDownListFor(). public static MvcHtmlString EnumDropDownListFor<TModel, TEnum>( this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression, object htmlAttributes) { ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); IEnumerable<TEnum> values = Enum.GetValues( typeof (TEnum)).Cast<TEnum>(); IEnumerable<SelectListIte

Model Binding With ASP.NET MVC @Html.ListBoxFor()

Image
Hi, In this tutorial, I will present three ways on model binding with the @Html.ListBoxFor() helper using a ViewModel class and a simple model class. The Index view below has three @Html.ListBoxFor() controls inside a form that will be populated with different ways using MultiSelectList class, List<SelectListItem> , and IEnumerable<CountryInfo> . @using (Html.BeginForm("Index", "Home", FormMethod.Post)) { <div class= "form-group" > <div class= "form-group" > @Html.ListBoxFor(x => x.SelectedPhoneNumbers, Model.phoneNumbers, new { @class = "form-control " }) </div> <div class= "form-group" > @Html.ListBoxFor(x => x.SelectedItemIds, Model.Items, new { @class = "form-control " }) </div> <div class= "form-group" > @Html.ListBoxFor(x => x.Country.SelectedCountry, new MultiSelectList(Model.Country.Countries, "Code", "T

Read Or Parse Or Deserialize JSON Using JavaScriptSerializer Class In C#

Hello, Here's a simple demonstration on how to parse JSON data using .NET's JavaScriptSerializer class. Given the sample JSON data below: { "catalog": { "book": [ { "id": "bk101" , "author": "Gambardella, Matthew" , "title": "XML Developer's Guide" , "genre": "Computer" , "price": "44.95" , "publish_date": "2000-10-01" , "description": "An in-depth look at creating applications with XML." }, { "id": "bk102" , "author": "Ralls, Kim" , "title": "Midnight Rain" , "genre": "Fantasy" , "price": "5.95" , "publish_date": "2000-12-16" , "description": &q

Return View() Statement Not Redirecting To View In ASP.NET MVC Using $.ajax() Post.

Hello, Normally, you submit client-side data to a controller action via @Html.BeginForm(), then perform processing statements and lastly invoke the return View(); statement inside the controller action which will redirect you to the view which basically works. However, in a scenario where-in you will post data to a controller action using jQuery Control Event such as Button Click , the return View() statement in the controller action won't redirect to the specified view given the sample controller action below. [HttpPost] public ActionResult UpdatedEmpTrainings( string empId) { _context = new EmployeeEntities(); model = new List<EmployeeTrainingsViewModel>(); model = ( from emp_trainings in _context.EmployeeTrainings.AsEnumerable() join training in _context.Trainings.AsEnumerable() on emp_trainings.TrainingID equals training.TrainingID where emp_trainings.EmployeeID == Convert.ToInt32(empId) select new EmployeeTrainingsViewModel {

Method Line Separator in Visual Studio 2013

Image
Hello, There was a question in the forums on how to add a line separator in every method written in Visual Studio 2013 C# IDE. Well, there's a plugin for Visual Studio called Productivity Power Tools 2013 which you can download and install. After installing it, then proceed to making changes to your IDE. To show line separator in your Visual Studio 2013 IDE, perform the steps below: Click on Tools Menu -> Options -> Productivity Power Tools -> Other Extensions -> Show a separator between methods in the Editor Screenshot: Note: Make sure to restart Visual Studio to affect those changes. Reference: Visual Studio 2013 Goodies

Change DataGridViewRow FontStyle To Bold In CellFormatting Event

Hello, There was a question raised in the forums on how to change the FontStyle of a DataGridViewRow to Bold in the CellFormatting event. A solution is to set the Font property of a row with a Font object setting it's second parameter to FontStyle.Bold. private void EmployeeDTRDataGridView_CellFormatting( object sender, DataGridViewCellFormattingEventArgs e) { if (EmployeeDTRDataGridView.Columns[e.ColumnIndex].Name == "OnLeave" ) { if (e.Value != System.DBNull.Value) { if (Convert.ToBoolean(e.Value) == true ) { EmployeeDTRDataGridView.Rows[e.RowIndex].DefaultCellStyle.Font = new Font(e.CellStyle.Font, FontStyle.Bold); } } } }

Rounded Corner Button Example In WPF

Image
In WPF, you can alter the corner radius of a button using Style class. In Setter class, set the Property value to Template. Inside the Setter class, add a ControlTemplate class that will target the Button control. After that, you can add a Border class and then setting it's CornerRadius property with an integer value of the desired radius. The example below is a user control that has a Button and a Style resource that will alter the Button's corner radius. User Control <UserControl x:Class= "WPFButtonCornerRadiusXAMLVBForums.UCRoundedButton" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc= "http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d= "http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable= "d" d:DesignHeight= "45"

Using Bootstrap Typeahead.js Plugin In ASP.NET MVC Project

Image
Hello all, Bootstrap has lots of plugins that you can experiment with, and one of them is the Typeahead.js plugin similar to jQueryUI Autocomplete plugin. According to Bootstrap, Typeahead is an extendend plugin for quickly creating elegant typeaheads with any from text input. So given the description of the widget, I will provide a tutorial that integrates the plugin in an ASP.NET MVC project basing from this article Twitter Bootstrap Typeahead and ASP.NET MVC . The author from the source demonstrates preloaded country values but in my case, I modified it to handle searching through a database. So to proceed with, just follow the steps below: 1. Create an ASP.NET MVC Project (C#). 2. Add an ADO.NET Entity model that connects to the AdventureWorks DB and it's CountryRegions table. 3. In your home controller, add the code that search countries based on a given value. private static AdventureWorks2012Entities _context; // // GET: /Home/ public ActionResult Index() { ret

Find Elements In XML From List Collection Using LINQ And C#

Hello, Given you have a List object with a number of string elements and you want to check whether the elements in the list are found in the XML file, options are your going to use the for each loop or the List.ForEach() action to traverse the List and perform searching through the XML file. XML File <?xml version="1.0" encoding="utf-8" ?> <catalog> <book id= "bk101" > <author>Gambardella, Matthew</author> <title>XML Developer's Guide</title> <genre>Computer</genre> <price>44.95</price> <publish_date>2000-10-01</publish_date> <description> An in-depth look at creating applications with XML. </description> </book> <book id= "bk102" > <author>Ralls, Kim</author> <title>Midnight Rain</title> <genre>Fantasy</genre> <price>5.95</price>

How To Read Or Parse XML Using XDocument In LINQ And C#

Given the XML file below which is a sample popularized my Microsoft, here's how to traverse with that XML file using LINQ to XML XDocument class. XML File <?xml version="1.0" encoding="utf-8" ?> <catalog> <book id= "bk101" > <author>Gambardella, Matthew</author> <title>XML Developer's Guide</title> <genre>Computer</genre> <price>44.95</price> <publish_date>2000-10-01</publish_date> <description> An in-depth look at creating applications with XML. </description> </book> <book id= "bk102" > <author>Ralls, Kim</author> <title>Midnight Rain</title> <genre>Fantasy</genre> <price>5.95</price> <publish_date>2000-12-16</publish_date> <description> A former architect battles corporate zombies, an ev

Donate