Castle Monorail Complex Binding

525 views Asked by At

I have this ViewModel

class ProductViewModel{
List<ProductSellingScopeViewModel> Scopes{get;set;}
string Name{get;set;}
int Id{get;set;}
}


class ProductSellingScopeViewModel{
int IdScope{get;set;}
decimal Price{get;set;}
}

 class SellingScopeViewModel{
    int Id{get;set;}
    string Name{get;set;}
    }

For creating/updating a product I'll have a checkbox for each SellingScopeViewModel (for instance "web site, "catalogue" ...) and the user will select the scopes in witch he'd like to sell the product, and for each scope he'll write a price.

I tried this (only the price part) (NVelocity):

#set($checkBoxList = $FormHelper.CreateCheckboxList("product.Scopes", $Scopes,"%{text='Name',value='Id',sourceProperty='IdScope'}"))
            #foreach($elem in $checkBoxList) 
$checkBoxList.Item("$elem.Id") $Form.LabelFor("$elem.Id",$elem.Name)
            #end

#foreach($aScope in $Scopes) 
$FormHelper.LabelFor("product.Scopes[$velocityCount].Price","$aScope.Name")
$FormHelper.TextField("product.Scopes[$velocityCount].Price")
$FormHelper.HiddenField("product.Scopes[$velocityCount].IdScope")
#end

For creating a product there is no problem.

But for updating a product the bining is not working because $product.Scopes migh not be in the same order as $Scopes, or $product.Scopes might not be linked with every scope. So I'll end up with

Catalogue : |20$|
Call Center : |25$|
Web site : ||

If my objects are like this

$Scopes :

Id - Name
---------
1 - Catalogue
2 - Call Center
3 - Web Site

And Product.Scopes

IdScope - Price
--------------
3 - 20$
1 - 25$

I thought of changing ProductViewModel.Scopes into a Dictionnary (and use the scope id instead of $velocityCount) but i'm not sure how the framework will react. Or not using the framework for showing the price values and deal with an helper, but that's kinda fighting against the framework.

What do you think ?

EDIT : Castle Monorail's FormHelper will throw an exception if I try to create a checkboxlist with my Dictionnary...

1

There are 1 answers

1
jakobandersen On

Im not sure i get your problem, but why the two loops, you can put your price information in the loop where you print the checkboxes:

           #set($checkBoxList = $FormHelper.CreateCheckboxList("product.Scopes", $Scopes,"%{text='Name',value='Id',sourceProperty='IdScope'}"))
            #foreach($elem in $checkBoxList) 
               $checkBoxList.Item("$elem.Id") $Form.LabelFor("$elem.Id",$elem.Name)
               $FormHelper.LabelFor("product.Scopes[$velocityCount].Price")
               $FormHelper.TextField("product.Scopes[$velocityCount].Price")
               $FormHelper.HiddenField("product.Scopes[$velocityCount].IdScope")
            #end