0

如何将使用Html.RadioButton()HTML 帮助程序生成的单选按钮的值绑定到具有结构作为类型的字段?

不那么抽象:

CommonProject.Services.SearchBag.Effects:

public enum Effects
{
    Any,
    Solid,
    Effect
}

在强类型 ViewData 中:

public class SearchBag{    
    public Effects EffectIndicator { get; set; }
}

在我看来(这实际上不起作用):

<%=Html.RadioButton("SearchBag.EffectIndicator", "Any", ViewData.Model.SearchBag.EffectIndicatorIsAny, new { @id = "SearchBag.EffectIndicatorAny" })%>

更新
它似乎工作一次..
最初它根据需要创建单选按钮,然后当您更改值并回发时,该值被正确绑定。然后在重新生成页面时,按钮的所有值都设置为您之前选择的值。

4

1 回答 1

1

如果您的视图是使用 SearchBag 作为您的视图数据类的强类型,那么您应该能够按照以下方式执行一些操作:

<%= Html.RadioButtonFor(model => model.EffectIndicator, "Any", new { @id = "SearchBag.EffectIndicatorAny" }) %>

然后,当您的视图表单提交回控制器时,它看起来像:

public class MyController : Controller
{
    public ActionResult MyActionMethod(SearchBag searchBag)
    {
        Effects selectedEffect = searchBag.EffectIndicator;
    }
}

这有帮助吗?

于 2010-11-19T22:49:40.857 回答