Showing posts with label MVC Preview 3. Show all posts
Showing posts with label MVC Preview 3. Show all posts

Thursday, August 28, 2008

ASP.NET MVC HtmlHelper DropDownList

If you've been working with ASP.NET MVC Preview 3, or later, you may have wondered how to create a drop down list.

From within your controller, you define the System.Web.Mvc.SelectList for the DropDownList. Think of the SelectList as all of the information that the html control will need to contain your list, set the text and value field, and specify the selected item.

Example:
Controller:
var dbContext = new TriathlonDataContext();

var stateProvinceList = from sp in dbContext.StateProvinces
where sp.IsActive == true
orderby sp.StateProvinceCode
select sp;

ViewData["StateProvinceID"] = new SelectList(stateProvinceList.ToList(), "StateProvinceCode", "Name");

View:

The end result is a working drop down list, like the one pictured below:

Tuesday, August 26, 2008

ASP.NET MVC HtmlHelper ReadOnly TextBox

If you've been working with ASP.NET MVC Preview 3, or later, you may have wondered how to create a readonly textbox control using the HtmlHelper class.

You can accomplish this using an anonymous object initializer with the property "ReadOnly" for the htmlAttributes parameter.

Here is an example:
Html.TextBox("UrlID", new {ReadOnly="ReadOnly"})

You can also specify other arguements for the rendered Html input control as part of the htmlAttributes parameter, just by including them in the anonymous object initializer.

Here are a few other examples to help you out:
new { _class="cssClass", onclick="alert('Alert!')" })

Monday, August 25, 2008

ASP.NET MVC HtmlHelper and CheckBoxExtensions (Html.CheckBox)

I've been working with Preview 3 of ASP.NET MVC, and started working with the HtmlHelper class.

These are the methods for generating an HTML controls with the HtmlHelper class. Very useful for developing view, entry, and edit views.

This is a short (and incomplete) list of some of the methods available for generating html controls:
string ActionLink - Generate html link to an action
string AttributeEncode - Html encode attribute
string DropDownList - Html dropdown list
string Encode - Html encode value
string Hidden - Html input of type hidden
string ListBox - Html listbox
string Password - Html input of type password
string RouteLink - Link to a specific route
string TextBox - Html input
string CheckBox [Extension method from System.Web.Mvc.CheckBoxExtensions]
string TextArea [Extension method from System.Web.Mvc.TextInputExtensions]

This article is specifically about the CheckBox extension method, and how to use it.

The CheckBox extension method are contained within the
CheckBoxExtensions class located in the System.Web.Mvc namespace.

There were a number of posts out there about using a boolean value, and having that value correctly transformed back into a boolean using the BindingHelperExtensions.

The trick to correctly using this control for this purpose, is to set the value to "true", and to set the ischecked parameter to your current state.

Example

Html.CheckBox("IsActive", "Is Active", "true", ViewData.Model.IsActive)