Nagareboshi
Nagareboshi

Reputation: 39

Optional bool parameters in mvc controller

Here's what I want to have:

 [GET("page/{id}/")]
    public ActionResult Execute(Guid id, bool x = false, bool y = false, 
                                         bool z = false)

The reason I want to have the bools optional is so I can later use this method like this:

return RedirectToAction<SomeController>(c => c.Execute(id, y: true));

Unfortunately when I try to build the solution I get these errors: "An expression tree may not contain a call or invocation that uses optional arguments" and "An expression tree may not contain a named argument specification".

My question is: is it possible to do something like that with controllers? What about optional parameters in routing?

Upvotes: 1

Views: 2473

Answers (1)

AdaTheDev
AdaTheDev

Reputation: 147324

You could use a model class instead. Something like:

public class ExecuteModel
{
    public Guid id {get;set;}
    public bool x {get;set;}
    public bool y {get;set;}
    public bool z {get;set;}
}

and change your Action to:

public ActionResult Execute(ExecuteModel model)
{
   ...
}

and your redirect would become:

return RedirectToAction<SomeController>(c => c.Execute(
    new ExecuteModel{id=id, y=true}));

Upvotes: 2

Related Questions