Showing posts with label MVC. Show all posts
Showing posts with label MVC. 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)

Wednesday, August 20, 2008

Sample MVC Site

Just for fun, I created a simple MVC site using the ASP.NET MVC Framework Preview 3.

It was fairly easy to use, has some great examples out there, and took less time to develop the basic site than I thought (from running Visual Studio to deploying the code ~1 hour)

Here is the total sum of the learning curve to get the basics:
1. Watch the ASP.NET MVC Preview 3 Videos on http://www.asp.net/mvc/. The videos are done by Scott Hanselman who walks you though some basic concepts and sites. The sourcecode is available, and you can download it from the provided link.
2. Check out the ASP.NET MVC Storefront Starter Kit. I found this to be a big help to me, I just basically watched a few of the videos, and picked up the needed information.

Here is the link in case your interested: http://www.chrisghardwick.com

The site has some additional funcationality and sections that I did not publish in the first run. I am going to keep working on those, and get them added at a later date.

ASP.NET MVC

Model View Controller

The ASP.NET MVC framework in now in Preview 3. If you are interested in testing out the framework, you can download it from here.

If your interested in learning more about Model-view-controller, there is a great article on Wikipedia here.

A Model-view-controller as a design pattern is not a new concept. Basically the concept is that the pattern seperates the business logic and user interface from each other, allowing for seperation of concerns (extreemly useful for unit-testing). MVC is not limited as a design pattern to web development, but the new ASP.NET MVC framework is.

From http://www.asp.net/mvc/ "ASP.NET MVC provides a framework that enables you to easily implement the model-view-controller (MVC) pattern for Web applications. This pattern lets you separate applications into loosely coupled, pluggable components for application design, processing logic, and display."

When I have more time, I'll publish additional MVC content.