質問

I have a custom helper defined in ASP.Net MVC 4 project as below

public static class CustomHelpers
{
    public static string UserNameForID(int UserIDValue)
    {
        // do database operation and return record for UserIDValue
        return "ABC";
    }
}

In my View I have the code

@model ABC.DataAccess.Product
@CustomHelpers.UserNameForID((model=>model.LAST_UPDATE_BY))

the above code gives this error:

"Cannot convert lambda expressions to type 'int' becasue it is not a delegate type.

enter image description here

However if I test the above code a

@CustomHelpers.UserNameForID(1)

the application returns the string ABC as expected .

How to resolve this error? I would like to pass the value stored in LAST_UPDATE_BY of the model.

役に立ちましたか?

解決 2

model=>model.LAST_UPDATE_BY generates an argument from type Expression<...>. (For a sample see Html.EditorFor().

your CustomHelpers Method wants an argument of type int, so you have to call it like:

@CustomHelpers.UserNameForID(Model.LAST_UPDATE_BY)

他のヒント

You're trying to cast an expression to the result of that expression. Just use:

@model ABC.DataAccess.Product
@CustomHelpers.UserNameForID(Model.LAST_UPDATE_BY)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top