0

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.

Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
Dilara Albayrak
  • 314
  • 1
  • 3
  • 23
  • 2
    You have multiple problems - you cannot use a `foreach` loop to bind controls to a collection (refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943)). 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`) –  Oct 20 '16 at 09:38
  • 1
    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 Oct 20 '16 at 09:40
  • 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 Oct 20 '16 at 10:01
  • 1
    You need to remove `new { @checked = "..." }` –  Oct 20 '16 at 10:03
  • ok, then how can i define checked(i'm specifically asking because all examples i see were like that). – Dilara Albayrak Oct 20 '16 at 10:04
  • 1
    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 –  Oct 20 '16 at 10:06
  • Thank you so much! – Dilara Albayrak Oct 20 '16 at 10:07
  • And your property should be a `bool` because it can have only 2 states (true or false) –  Oct 20 '16 at 10:08
  • i had a same issue. i fixed with this: https://stackoverflow.com/a/23383159/3928982 – Mahmut Gundogdu Jul 14 '17 at 15:02

1 Answers1

0

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.

Dilara Albayrak
  • 314
  • 1
  • 3
  • 23