C # Razor - error when saving a form with radio button

1

I have a problem saving a form by a radio button, it always saves the value zero and must be on demand according to what the user selects:

<div class="form-group">
    <label for="BONUS" class="col-md-2 control-label">Bono:</label>
    <div class="col-md-10">

        if (Model.BONUS== 1)
            {
            <text>
            <label class="radio radio-inline"><input type="radio" name="BONUS" value="@Model.BONUS">No</label>
            <label class="radio radio-inline"><input type="radio" name="BONUS" checked="checked" value="@Model.BONUS">Si</label>
            </text>
            }
            else if (Model.BONUS == 0)
            {
            <text>
            <label class="radio radio-inline"><input type="radio" name="BONUS" checked="checked" value="@Model.BONUS">No</label>
            <label class="radio radio-inline"><input type="radio" name="BONUS"  value="@Model.BONUS">Si</label>
            </text>
            }
            else
            {
            <text>
            <label class="radio radio-inline"><input type="radio" name="BONUS" value="0">No</label>
        <label class="radio radio-inline"><input type="radio" name="BONUS" value="1">Si</label>
            </text>
            }
        }
    </div>
</div>

Please if someone can help me with the error.

    
asked by DAES 29.03.2018 в 05:45
source

1 answer

0

The problem is that in the first two cases ( Model.BONUS == 0 and 1 ) the value of each input is @Model.BONUS regardless of the "Yes" and "No" that you have in both cases.

all you have to do is leave the values 0 and 1 in the attributes value of each.

Additionally, one way to reduce the code would be:

<div class="form-group">
    <label for="BONUS" class="col-md-2 control-label">Bono:</label>
    <div class="col-md-10">
        <label class="radio radio-inline">
            <input type="radio" name="BONUS" value="0" @if(Model.BONUS == 0) {<text>checked="checked"</text>}/>No
        </label>
        <label class="radio radio-inline">
            <input type="radio" name="BONUS" value="1" @if(Model.BONUS == 1) {<text>checked="checked"</text>}/>Si
        </label>
    </div>
</div>
    
answered by 29.03.2018 / 20:51
source