views:

126

answers:

1

I have the following macro:

<macro name="InputField" id="string" value="string">
...
      <input type="text" id="${id}" name="${id}" value="${value}  />
...
</macro>

And the call to the macro:

${InputField( "model.address.address1", 75, "Address", model.Address.Address1 )}

The only problem is that model.Address will be null in some situations (creating the item instead of editing it), because of this the macro doesn't run or fails and just outputs the macro call to the view.

How can I pass either "" or the value of model.Address.Address1 depending if Address is null or not? the null operator ($!{}) doesnt seem to work in this instance.

+2  A: 

Solution 1. Write method

public static string HandleNull(Func<object> func)
{
   try { return func().ToString(); } 
   catch (NullReferenceException) { return ""; }
}

and use it instead of Spark macro.

${InputField( "model.address.address1", 75, "Address", HandleNull(() => model.Address.Address1) )}

Solution 2. Use http://www.jmill.net/taxonomy/term/312

Solution 3.

<macro name="InputField" id="string" value="Func<string>">
...
      <input type="text" id="${id}" name="${id}" value="$!{value()}  />
...
</macro>

${InputField( "model.address.address1", 75, "Address", () => model.Address.Address1 )}

All the solutions depend on deferred execution.

queen3