I have the following MVC Action:
[ActionName("order-brochure")]
public ActionResult OrderBrochure(int brochureId, bool isModal = true)
{
return View(HomeModelBuilder.BuildOrderBrochureModel(brochureId, isModal, false));
}
Where isModal
is an optional parameter
Is there any other way to set the default for this other than using bool isModal = true
within the method as my company uses code analysis and this fails giving a CA1026: default parameters shouldn't be used
However, I can't override the method as you can't have two actions with the same name as you'll get an error along the following lines:
The current request for action 'order-brochure' on controller type 'HomeController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult OrderBrochureModal(Int32, Boolean) on type MarshallsCoUkCommercial.Website.Controllers.HomeController
System.Web.Mvc.ActionResult OrderBrochure(Int32) on type MarshallsCoUkCommercial.Website.Controllers.HomeController
And if I use
public ActionResult OrderBrochure(int brochureId, bool isModal)
Setting isModal
to UrlParameter.Optional
I can't test to see if isModal
is null as it will always be set to false
Apparently this rule is outdated and should not be applied to C# 4.0 and above. See this discussion for similar question.
I would say just disable this rule, as it makes no sense in MVC4.
However, suppressing the rule might not be compliant with your company rules, then you'd have to work around that. (These rules are silly, when you have to hack around some outdated crap like that!)
One of the options is to have a custom rule for the every case like that: specify default value for optional param. (Sorry, can't find implementation for that quickly)
Or, as already suggested go with nullable boolean
public ActionResult OrderBrochure(int brochureId, bool? isModal)
and then in your method check for null on isModel
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With