0
votes

I've an Hotel model and there is a HotelPrice list in the model.

What i'm trying to do is when user display hotel detail page let hotel prices' info be shown, also they can be editable. I've a breakfastIncluded property on HotelPrice instances which i have to display as radio button.

Here is my code:

<div>
@{
foreach(var hotelPrice in Model.HotelPrices)
{
    <label class="mt-radio-inline>
        @Html.RadioButtonFor(hotelModel => hotelPrice.breakfastIncluded, "1", new { @checked = (hotelPrice.Breakfast.ToString() == "1") })Yes
        <span></span>
    </label>
    <label class="mt-radio-inline>
        @Html.RadioButtonFor(hotelModel => hotelPrice.breakfastIncluded, "0", new { @checked = (hotelPrice.Breakfast.ToString() == "0") })No
        <span></span>
    </label>
}
}

When i display my page(i could not come editing part, yet), all breakfastIncluded properties' radio buttons are displayed as unassigned and the last one's radio button is displayed wrongly(i.e it has to be "Yes" but it seems like "No").

However, when i checked by Inspect tool of browser, all of the radio buttons' checked property appears correct(i.e if it should be "Yes" that radio button's checked is true and "No"'s checked is false).

I'm new to html and i could not figure out why that happens. Can you help me? Thanks in advance.

1
You have multiple problems - you cannot use a foreach loop to bind controls to a collection (refer this answer). And you do not set the checked attribute (the RadioButtonFor() will do that correctly when you fix your view (and checked="true" or checked="false" or checked="anything" are all the same - they sets the checked state but since only one can be checked, the last one is (and the property should be a bool, not an int) - user3559349
An important lesson in MVC Razor is to NOT use foreach when looping through collections to create Form elements. Always use e.g. for (var i = 0; i < Model.HotelPrices.Count; i++) and then @Html.RadioButtonFor(m => Model.HotelPrices[i], ... ). This is needed to generate element names that can be used by the model binding when the form is submitted. - Peter B
Thanks both @StephenMuecke & @PeterB . Using for instead of foreach changes positively(i.e now at least something appears). I set checked part of radio (just to see) as for "Yes" new { @checked = "true" }, for "No" new { @checked = "false" }. Now all values appears as "No" whereas they have to appear as "Yes". @StephenMuecke i couldn't understand how i should correct checked parts. Also i couldn't understand what do you mean by saying " the last one is (and the property should be a bool, not an int)". If you can re-explain i'd be appreciated. - Dilara Albayrak
You need to remove new { @checked = "..." } - user3559349
Because the RadioButtonFor() method sets it correctly - it the value of the property is 1 the first one will be checked. If its 0 then the 2nd one will be - thats how model binding works - it binds to the value of your property - user3559349

1 Answers

0
votes

By applying solutions in comments. I used for instead of foreach and it appears that i shouldn't set checked value since the radio buttons have values.