This project is read-only.

Trying to convert @helper to a shape for use with @Display

Topics: General, Troubleshooting
Apr 4, 2013 at 9:21 PM
Edited Apr 4, 2013 at 9:21 PM
I had this @helper function in a Helpers.cshtml file located under ~/App_Code:
@helper TempCost(Decimal cost)
@: @String.Format("{0:C0}", cost)
Which I previously called in my view, thusly:
PriceInfrormation is my model, and MyPrice is a property within (decimal).

Because we cannot use ~/App_Code, I now want to put this helper in it's own shape. I believe I can call the shape and pass the arguments with no problem like this:
I also tried:
However, I do not know how to get the code in the shape to work. The shape is located under ~/Views. I know the shape is rendered because I just inserted some random text and it's showing up in the view.

I tried something like:
decimal cost;
@String.Format("{0:C0}", cost)
but am getting the following error:

error CS0165: Use of unassigned local variable 'cost'

Otherwise, if I try something like:
    decimal? cost = null;
@String.Format("{0:C0}", cost)
The page is rendered and the shape is as well, but it's just blank in the area where something would ordinarily be rendered.

Sorry, not a programmer by profession. Appreciate any help on how I might convert this.
Apr 4, 2013 at 9:27 PM
You need to specify a property to assign the "cost" to, when passing the information to the shape:
@Display(New.TempCost(Cost: PriceInformation.MyPrice))
And in the template, declare the model as dynamic type, and reference the "Cost" property on the model:
@model dynamic;
@String.Format("{0:C0}", Model.Cost)
Apr 5, 2013 at 2:07 AM
Edited Apr 5, 2013 at 2:07 AM
Thanks. That worked. I was trying something similar but was using "decimal" instead of "dynamic" and completely left out "Cost":
//This fails
@model decimal
@string.Format("{0:C0}", Model)
FYI, for anyone else in my shoes, leave out the ";" - it's just "@model dynamic".