Getting values of check-box from formcollection in asp.net mvc

后端 未结 2 2012
难免孤独
难免孤独 2021-01-27 01:55

I viewed some topics here but I still have a problem with getting values from checkboxes.

Part of Model :

public Dictionary TargetL         


        
2条回答
  •  旧巷少年郎
    2021-01-27 02:29

    Create all the checkboxes with the same name. In this sample I'm using 'SelectedTargetLanguages'.

    @using (Html.BeginForm())
    {
        foreach (var item in Model.TargetLanguages)
        {
            
        }
        
    @Html.SubmitButton("Actualizar listado") }

    Then, in your action the parameter must be an array of strings like this:

    public ActionResult AddDictionary(string[] selectedTargetLanguages)
    

    Note that the name of the argument is the same name of the checkboxes. (It works even with the different casing).

    You should use explicit arguments like this, rather than the generic FormCollection. Anyway, if you use FormCollection, you shpuld also receive the array.

提交回复
热议问题