Click here to Skip to main content
15,896,500 members
Articles / Web Development / ASP.NET

ASP.NET MVC 2 Basics - Working with ListBoxes

Rate me:
Please Sign up or sign in to vote.
4.83/5 (17 votes)
12 Jan 2011CPOL7 min read 112.8K   4.5K   35  
Demonstrates moving items between two listboxes in ASP.NET MVC 2.
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<ListBoxDemo.Models.ViewModel>" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Index</title>    
</head>
<body>
    <h2>Christmas gift list</h2>
    
    <p>Select up to 3 gifts from the list below up to a total value of 400</p>
    
    <%using(Html.BeginForm()){ %>
        <div>
            <table>
                <thead>
                    <tr>
                        <th>Available</th><th>&nbsp;</th><th>Requested</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td valign="top">
                            <%=Html.ListBoxFor(model => model.AvailableSelected, new MultiSelectList(Model.AvailableProducts, "Id", "Name", Model.AvailableSelected), new { size = "6" })%>
                        </td>
                        <td valign="top">
                            <input type="submit" name="add" id="add" value=">>" /><br />
                            <input type="submit" name="remove" id="remove" value="<<" />
                        </td>
                        <td valign="top">
                            <%=Html.ListBoxFor(model => model.RequestedSelected, new MultiSelectList(Model.RequestedProducts, "Id", "Name", Model.RequestedSelected))%>
                        </td>
                    </tr>
                </tbody>
            </table>
            <br />
            <%=Html.HiddenFor(model=>model.SavedRequested) %>                    
            
            <fieldset>
                <legend>Your wish list</legend>
                <table>
                    <thead>
                        <tr>
                            <th>Product</th>
                            <th>Description</th>
                            <th>Price</th>
                        </tr>
                    </thead>
                    <tfoot>
                        <tr>
                            <td colspan="2">&nbsp;</td>
                            <td><span id="total" style="font-weight:bold;"><%=Model.RequestedTotal.ToString("G") %></span></td>
                        </tr>
                    </tfoot> 
                    <tbody id="tableBody">
                    <%foreach(var product in Model.RequestedProducts) { %>
                        <tr>
                            <td><%=product.Name %></td>
                            <td><%=product.Description %></td>
                            <td><%=product.Price.ToString("G") %></td>
                        </tr>
                    <%} %>
                    </tbody>                         
                </table>
            </fieldset>
            <input type="submit" name="send" id="send" value="Send to Santa!" />
            <%=Html.ValidationSummary() %>
        </div>
    <%} %>
</body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) AJ Systems
United Kingdom United Kingdom
I am currently a developer at a management consultancy company.
In addition to all things .NET I have a keen interest in Japan and the Japanese language and hope to be able to combine these passions one day soon.

Comments and Discussions