Home Using a ternary operator in a lambda expression gives "Only Assignment, Call, Increment, Decrement ... as a statement" exception
Reply: 0

Using a ternary operator in a lambda expression gives "Only Assignment, Call, Increment, Decrement ... as a statement" exception

user960
1#
user960 Published in June 20, 2018, 1:31 am

I'm getting a "Only Assignment, Call, Increment, Decrement, Await Expression and New Object expressions can be used as a statement" error with the following ternary operator:

@using (Html.BeginForm<AssetController>(
           x => (Model.Id == -1 ? x.Create() : x.Edit(Model.Id) ) , 
           FormMethod.Post, 
           new { @class = "form-horizontal", id = "save-assetType-form" }))

And a "A lambda expression with a statement body cannot be converted to an expression tree" error for the following code:

@using (Html.BeginForm<AssetController>(x => 
    {
        if (Model.Id == -1) 
            x.Create();
        else 
            x.Edit(Model.Id);

    }, FormMethod.Post, new { @class = "form-horizontal", id = "save-assetType-form" }))
}

Is there a way to achieve concise conditional logic in my lambda here? Having trouble with the syntax.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.31671 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO