0

该模型是 RatingSource 的 IEnumerable

public class RatingSource
    {
        public int Id { get; set; }

        public string Source { get; set; }

        public bool IsActive { get; set; }
    }

查看代码:

@foreach (var ratingSource in Model)
 {
     <tr>
        <td>
            @Html.DisplayFor(modelItem => ratingSource.Source)  
        </td>
        <td>
             @Html.RadioButton("Active",ratingSource.Id,ratingSource.IsActive)
        </td>
     </tr>
 }

我不为 IsActive 使用 EditorForModel,因为它提供了复选框。如何将编辑的数据保存到数据库?我使用实体框架。

4

1 回答 1

0

这就是我能够获取所选 RatingSource 的方式

控制器代码:

    public ViewResult SaveInfo()
    {            
        return View(lstRatingSources);
    }

    [HttpPost]
    public ViewResult SaveInfo()
    {
        var formValue = Request.Form["Active"];
        int Id = Convert.ToInt32(formValue);
        RatingSource selectedRatingResource = 
                     lstRatingSources.FirstOrDefault(p => p.Id == Id);
        return View("AcknowledgementView");
    }

查看代码:

@using (Html.BeginForm())
{
<table>
@foreach (var ratingSource in Model)
{
     <tr>
        <td>
            @Html.DisplayFor(modelItem => ratingSource.Source)  
        </td>
        <td>
             @Html.RadioButton("Active", ratingSource.Id, ratingSource.IsSelected)
        </td>
     </tr>
}
<tr><td><input type="submit" value="Save Info" /></td><td></td></tr>
 </table>
}

请不要问我为什么使用Request.Form["Active"],这是凌晨 2 点,我没有去寻找细节。只需快速查看 Request.Form 对象即可。希望这对您有所帮助。

于 2013-05-18T06:15:06.813 回答