-2

I'm using Visual studio 2013 with C#. I have a simple web page that I want to display radio buttons for a section named Urgency.

The values for this Urgency section are sat in a table named urgency in SQL. I can successfully display these values from the database in the form of the @Html.DropDownListFor.

This is the code I have used for the @Html.DropdownList that works:

 @Html.DropDownListFor(model => model.client.Urgency, new SelectList(Model.allUrgencies, "Id", "Urgency", 0), " - Select Urgency -") 

I'm trying to convert the @Html.DropDownListFor to a @Html.CheckBoxFor, this is proving to be very difficult and I'm struggling to get this to work.

I have managed to transform the dropdownlist to the following code but this still produces errors.

@Html.CheckBoxFor(model => model.client.Urgency, new SelectList(Model.allU, "Id", "Urgency1"))

These are the errors:

Cannot implicitly convert type 'short?' to 'bool'

Cannot convert lambda expression to delegate type 'System.Func' because some of the return types in the block are not implicitly convertible to the delegate return type.

Here is my list in my Model.

 [Display(Name = "Urgency:")]
 public List<Urgency> allUrgencies { get; set; }

Here is a screen shot of my database table.

database screen shot

Any suggestions are welcome.

halfer
  • 19,824
  • 17
  • 99
  • 186
Betty Lazzari
  • 99
  • 1
  • 6
  • 13
  • 1
    use a viewmodel, don't use directly your data model – Ehsan Sajjad Feb 27 '17 at 12:23
  • 1
    http://stackoverflow.com/questions/19468666/how-to-use-checkboxlist-and-dropdownlist-in-mvc4-razor or http://www.c-sharpcorner.com/UploadFile/4d9083/creating-simple-checkboxlist-in-mvc-4-using-razor/ – Yawar Murtaza Feb 27 '17 at 12:25
  • If you are going to bind directly to that Class (rather than a ViewModel), you should consider making Urgency property non-null. – lazarus Feb 27 '17 at 12:34
  • can you add your full model? – Usman Feb 27 '17 at 13:43
  • `CheckBoxFor()` is for binding to a `bool`. And you question states you want radio buttons, not checkboxes. What are you actually trying to do here - select one `Urgency` or many? –  Feb 27 '17 at 22:06
  • And for an example of generating a list of checkboxes, refer [this answer](http://stackoverflow.com/questions/29542107/pass-list-of-checkboxes-into-view-and-pull-out-ienumerable/29554416#29554416) –  Feb 27 '17 at 22:08

3 Answers3

0

Try:

Why are you using CheckBoxFor, you need to use RadioButtonFor.

Loop through your list generating an @Html.RadioButtonFor for each item bound to the same model property it is representing.

lazarus
  • 371
  • 2
  • 10
0

I use the following approach supporting Select All/None feature and it works like a charm:

Model:

public class EditUserViewModel
{
    public EditUserViewModel()
    {
        this.GroupsList = new List<SelectListItem>();
    }

    // Add a GroupsList Property:
    public ICollection<SelectListItem> GroupsList { get; set; }
    
    //other properties
}

View:

<div class="form-group">
    @Html.Label("Groups", new { @class = "col-md-3 control-label" })
    <div class="col-md-6">

        <div class="checkbox">
            <label>
                <input type="checkbox" id="checkAll" name="" class="" />
                <span class="cr"><i class="cr-icon glyphicon glyphicon-ok"></i></span>
                <text id= "checkText">All?</text> 
            </label>
        </div>

        @foreach (var item in Model.GroupsList)
        {
            <div class="checkbox">
                <label>
                    <input type="checkbox" name="selectedGroups" value="@item.Value" />
                    <span class="cr"><i class="cr-icon glyphicon glyphicon-ok"></i></span>
                    @item.Text
                </label>
            </div>
        }
    </div>
</div>

<script>
    $("#checkAll").change(function () {
        var checked = $(this).prop("checked");
        $("input:checkbox").prop('checked', checked);
        $('#checkText').text(checked ? 'None?' : 'All?');
    });
</script>
Murat Yıldız
  • 11,299
  • 6
  • 63
  • 63
0

After a bit of playing around and suggestion to use the radiobutton option instead. i have managed to complete this task via the following code...

@Html.RadioButtonFor(model => model.client.Urgency, 1 ,new { id = "rbUrgent"})
@Html.Label("rbUrgent", "Urgent")

@Html.RadioButtonFor(model => model.client.Urgency, 2 ,new { id = "rbNotUrgent"})
@Html.Label("rbNotUrgent", "Not Urgent")
anatol
  • 1,680
  • 2
  • 24
  • 47
Betty Lazzari
  • 99
  • 1
  • 6
  • 13