Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

c# - Pass values of checkBox to controller action in asp.net mvc4

I want to test if the checkbox is checked or not from my action method. What I need is to pass checkbox value from view to controller.

This is my view:

@using (Html.BeginForm("Index", "Graphe"))
{
    <table style="width: 100%;" border="1">
        <tbody>
            <tr>
                <td>Responsable:</td>
                <td>
                    <select id="Responsables" name="responsables">
                        <option>Selectionnez --</option>
                    </select>
                </td>
                <td><input id="responsable" name="checkResp" type="checkbox" /></td>
            </tr>
            <tr> 
                <td><input type="submit" value="Afficher" id="ButtonSubmit"/></td>
                <td><input class="button" id="ButtonReset" type="button" value="Annuler"  /></td>
            </tr>
        </tbody>
    </table>
}

and I try this:

public ActionResult Index(string responsables, bool checkResp)
{
    Highcharts chart = new Highcharts("chart");

    if (responsables != null)
    {          
        if (checkResp)
            chart = Global();
        else
            chart = Resp(responsables);
    }
    else
        chart = Global();
    return View(chart);
}

But I receive this error:

Le dictionnaire de paramètres contient une entrée Null pour le paramètre ? checkAct ? de type non Nullable ? System.Boolean ? pour la méthode ? System.Web.Mvc.ActionResult Index(System.String, System.String, Boolean) ? dans ? Project.Controllers.GrapheController ?. Un paramètre facultatif doit être un type référence, un type Nullable ou être déclaré en tant que paramètre facultatif. Nom du paramètre : parameters

Translated:

The parameter dictionary contains a null entry for the "checkAct" parameter of non-nullable type "System.Boolean" for the method "System.Web.Mvc.ActionResult Index (System.String, System.String, Boolean) ”in“ Project.Controllers.GrapheController ". An optional parameter must be a reference type, a type Nullable or be declared as an optional parameter. Name of parameter: parameters

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If a checkbox is checked, then the postback values will contain a key-value pair of the form [InputName]=[InputValue]

If a checkbox is not checked, then the posted form contains no reference to the checkbox at all.

Knowing this, the following will work:

In the markup code:

<input id="responsable" name="checkResp" value="true" type="checkbox" />

And your action method signature:

public ActionResult Index(string responsables, bool checkResp = false)
{
    //Do Something
}

This will work because when the checkbox is checked, the postback will contain checkResp=true, and if the checkbox is not checked the parameter will default to false.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...