noobieDev
noobieDev

Reputation: 3143

not sure why this RadioButton isn't giving the proper value

I have an EF model, I'm pulling the data from. When I trace it I can see its getting the correct value, but something is wrong with my C# code because it doesn't have the value showing in the radio button.

enter image description here

Can you see what I'm doing wrong?

Model:

public class SearchPageModel
{ 
   ...
   public IEnumerable<string> RoadwayTypeRadioButton { get; set; }
   ...
   // populate radio buttons
    public IEnumerable<string> populatRadioList()
    {
        var query = (from d in myContext.lkp_roadwaytypes
                     select d.roadwaytype).ToList().Distinct();
        return query;
    }
} 

View:

<div>
    <b>Facility Type *:</b>
      @foreach (var facility in Model.RoadwayTypeRadioButton)
      { 
      <ul>
       <li>    
          @Html.RadioButton(facility, "0") 
       </li>
       </ul>
      }      
</div>

Controller:

public ActionResult Search()
{
    SearchPageModel spm = new SearchPageModel();          
    spm.RoadwayTypeRadioButton = spm.populatRadioList();

    return View(spm);
}

Upvotes: 0

Views: 46

Answers (1)

minerva
minerva

Reputation: 436

In your view, change your foreach

 <div>
     <b>Facility Type *:</b>
     <ul>
         @foreach (string facility in Model.RoadwayTypeRadioButton)
         {
             <li>
                 @Html.RadioButton("roadwayTypeButton", facility) @facility
             </li>
         }
     </ul>
 </div>

Upvotes: 2

Related Questions