Shortcircuit
Shortcircuit

Reputation: 71

VB.NET/VBHTML Multiple Expressions

Helper:

<Extension()>
    Public Function InputRow(Of TModel, TProperty)(ByVal helper As HtmlHelper(Of TModel),
                                                    ByVal exp1 As Expression(Of Func(Of TModel, TProperty)),
                                                    ByVal exp2 As Expression(Of Func(Of TModel, TProperty)),
                                                    ByVal exp3 As Expression(Of Func(Of TModel, TProperty)),
                                                    Optional cl As CL = Nothing,
                                                    Optional split As List(Of Integer) = Nothing) As MvcHtmlString

      Dim expressions As New List(Of Expression(Of Func(Of TModel, TProperty))) From {expression1, expression2, expression3}

      Return InputRow(helper, expressions, cl, split)
    End Function
    

VBHTML:

  @Html.InputRow(Function(x) Model.Test, Function(y) Model.Test1, Function(z) Model.Test2)

I don't understand TModel and TProperty very well and I can't find much on it. The issue I'm having is that variable Model.Test is an nullable Interger while Model.Test1 and Model.Test2 are nullable decimals. When I try to get the metadata of Model.Test it throws an exception.

Get Metadata:

metaData = ModelMetadata.FromLambdaExpression(exp, helper.ViewData)

Exception:

System.InvalidOperationException: 'Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.'

Behind the scenes it's trying to convert the Integer? to a Decimal?, but I don't know why. The Model.Test expression is:

{x => Convert(value(ASP._Page_Views_Test_vbhtml).Model.Test)}

The value wrapped in the Convert is throwing the exception, but I don't know why it's getting wrapped in a convert. If I remove exp2 and exp3 from the method parameters. The convert doesn't happen.

Side Note: Is it wrong to pass multiple expressions to one method? If not, is there a way to pass a list of TModel, TProperty expressions to the helper. Instead of having separate variables?

Thanks in advance for any assistance.

Upvotes: 0

Views: 46

Answers (1)

jmcilhinney
jmcilhinney

Reputation: 54427

I think the issue here is that you want to pass multiple expressions of arbitrary type with only a single generic type parameter. Because there's only one TProperty, your three expressions have to involve properties of the same type, e.g. all String or all Integer. If you want to be able to use three different types then you need three different generic type parameters:

Public Function InputRow(Of TModel, TProperty1, TProperty2, TProperty3)(
    helper As HtmlHelper(Of TModel),
    exp1 As Expression(Of Func(Of TModel, TProperty1)),
    exp2 As Expression(Of Func(Of TModel, TProperty2)),
    exp3 As Expression(Of Func(Of TModel, TProperty3)),
    Optional cl As CL = Nothing,
    Optional split As List(Of Integer) = Nothing) As MvcHtmlString

Upvotes: 1

Related Questions