不确定为什么这个RadioButton没有给出正确的价值

时间:2014-06-12 17:55:40

标签: c# asp.net-mvc radio-button

我有一个EF模型,我从中提取数据。当我跟踪它时,我可以看到它获得正确的值,但是我的C#代码有问题,因为它没有显示单选按钮中的值。

enter image description here

你能看出我做错了吗?

型号:

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;
    }
} 

查看:

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

控制器:

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

    return View(spm);
}

1 个答案:

答案 0 :(得分:2)

在您看来,改变你的foreach

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