I’m rather new to MVC app and i’m having issues with combining html helpers and controllers. I hope I’m exposing my problem well enough, so here goes:
So I have these checkboxes:
<p>
@Html.ActionLink("Create New", "Create")
@using (Html.BeginForm())
{
<p>
<label>
Obj Colors
</label>
Blue : @Html.CheckBox("blueColor", true)
Red : @Html.CheckBox("redColor", true)
Green : @Html.CheckBox("greenColor", true) <br/>
Black : @Html.CheckBox("blackColor", true)
White : @Html.CheckBox("whiteColor", true)
</p>
(...)
}
and I have this huge method in my controller:
public ActionResult SearchIndex(string objName, string objType, string objSymbol, string objValue, string artistName, bool blueColor, bool redColor, bool greenColor, bool blackColor, bool whiteColor, bool colorless)
{
(...)
if (blueColor || redColor || greenColor || blackColor || whiteColor || colorless)
{
(...)
}
(...)
}
But when I load the page where the SearchIndex is, since I’ve introduced these booleans, I keep having the crash mentioned in the title as soon as I enter the page. What went wrong? Please help me.
The error says it all, really. Bools cannot be null, and apparently blueColor is.
You could change the method signature to accept
Nullable<bool>like soOr, if you are expecting the value to be passed into the method from the view, wrap those helpers you posted in a form and make your controller action accept post requests
EDIT
Or, if you are trying to do this as a get, for some reason, you must specify that in the form declaration:
But (if you do not make the bool params Nullable’s) you will have to pass values into this method in order for the page to load
Your ActionLink must pass some value into the action for the non nullable bools like this: