2

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.

9
  • Is the action method a get or post?
    – Forty-Two
    Feb 6, 2013 at 20:15
  • How are you calling the view?
    – Forty-Two
    Feb 6, 2013 at 20:24
  • By an actionLink located at the "Home" page which calls the "SearchIndex" cshtml file.
    – hsim
    Feb 6, 2013 at 20:26
  • Can you post that link please? You'll need to pass in values for the bools through it
    – Forty-Two
    Feb 6, 2013 at 20:27
  • <li>@Html.ActionLink("OBJS", "SearchIndex", "Obj")</li> That is located in the menu, is that what you are looking for?
    – hsim
    Feb 6, 2013 at 20:29

2 Answers 2

5

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 so

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)
{

Or, 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

@using (Html.BeginForm( ))
{
    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)
}

[HttpPost]
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)
{

EDIT

Or, if you are trying to do this as a get, for some reason, you must specify that in the form declaration:

@using (Html.BeginForm("SearchIndex", "ControllerName", FormMethod.Get ))
{

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:

@Html.ActionLink("Cards", "SearchIndex", "Card", new {blueColor = true, redColor = false}, null)
1
  • Oh, sorry, I will edit my post as this is already done. And I don't want to have null, but rather true/false.
    – hsim
    Feb 6, 2013 at 20:13
0

Initialize bool in your API Signature with either true or false

public ActionResult SearchIndex(string objName, string objType, bool blueColor =false, bool redColor=true )
{

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.