1

This is my html-

<td>
   @{
     IEnumerable<SelectListItem> Brands = ViewBag.GetBrands;
     foreach (var item in Brands)
       {                                                                   
         @Html.CheckBox(item.Text, false)                                                                                                                                                                                 
         <label>@item.Text</label><br />
       }
    }
</td>

Im Posting this controller as JSON data (form collection). How can i get checkbox's text and value in form collection data in controller?

1

3 Answers 3

1

How can i get checkbox's text and value in form collection data in controller?

The correct approach is to use a view model instead of this IEnumerable<SelectListItem>. So basically your model could look like this:

public class BrandViewModel
{
    public string Text { get; set; }
    public bool Checked { get; set; }
}

and then add a property to your main view model (the one your view is strongly typed to) of type IList<BrandViewModel>:

public IList<BrandViewModel> Brands { get; set; }

and then it's pretty easy:

<td>
    @for (var i = 0; i < Model.Brands.Count; i++)
    {                                                                   
        @Html.CheckBoxFor(x => x.Brands[i].Checked)
        @Html.LabelFor(x => x.Brands[i].Checked, Model.Brands[i].Text)
        @Html.HiddenFor(x => x.Brands[i].Text)
    }
</td>

and finally you can get rid of any weakly typed FormCollection from your controller action and simply take the view model:

[HttpPost]
public ActionResult SomeAction(IList<BrandViewModel> brands)
{
    ...
}

or if there are also other properties you need to pass your controller action may take the main view model:

[HttpPost]
public ActionResult SomeAction(MainViewModel model)
{
    // the model.Brands collection will be automatically bound here
    ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Although I'm not sure if this is the correct approach to MVC checkboxes with none boolean values, it seems to work really well. Thanks.
1

I managed to get ID by -

@Html.CheckBox(item.Text, false, new {item.Value}) 

1 Comment

try @Html.CheckBox(item.Text, false, new {id="myCheckbox"}) I did something similar with CheckBoxFor() and it worked for me.
0

First You have to perform post back to server.

@using (Html.BeginForm("actionname", "controller",
                    FormMethod.Post))
//your code
@{
     IEnumerable<SelectListItem> Brands = ViewBag.GetBrands;
     foreach (var item in Brands)
       {                                                                   
         @Html.CheckBox(item.Text, false)                                                                                                                                                                                 
         <label>@item.Text</label><br />
       }
    }
<input type="submit" class="k-button" value="Submit" id="btnSubmit" name="btnSubmit" />
}

Now in the controller you will get the values using form collection

[HttpPost]
public ActionResult actionName( FormCollection collection)
{

collection.keys["checkbox"].value ... your code
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.