简体   繁体   中英

Why am I getting “Cannot convert null to 'ScheduleType' because it is a non-nullable value type” here?

I understand what the error means, but not in this context. VS's Error List is pointing to the 1st line below and saying

Cannot convert null to 'ScheduleType' because it is a non-nullable value type

@using MyNamespace.ViewModels.Workflow.Schedule

@model ScheduleType

<div class="form-group">
    <div id="scheduleTypeRadio">
        <div class="btn-group" data-toggle="buttons">
            @foreach (var value in Enum.GetValues(Model.GetType()).Cast<ScheduleType>())
            {
                <label class="btn btn-primary @if(Model == value) { <text>active</text> }">
                    @Html.RadioButton("scheduleType", value, new { onchange = "if($(this).prop('checked') === true) $(this).closest('form').submit();" })
                    @if (value == ScheduleType.OnDemand && ((bool?)ViewData["immediateMode"]) == true)
                    {
                        @:Immediate
                    }
                    else
                    {
                        @Html.DisplayFor(m => value)
                    }
                </label>
            }
        </div>
    </div>
</div>

So the exact line is

@using MyNamespace.ViewModels.Workflow.Schedule

and how does that line use ScheduleType as if it's nullable? Makes no sense.

looks like your model is value type (enum) - @model ScheduleType

and you use it to get the possible values - Enum.GetValues(Model.GetType())

i think you might want to use a object type of model, and also when getting the list of possible enum values, try: Enum.GetValues(typeof(ScheduleType))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM