2008-11-10 50 views
0

如何将使用Html.RadioButton() HTML助手生成的单选按钮的值绑定到具有类型为struct的字段?ASP.NET MVC中枚举的默认modelbinding?

减摘要:

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" })%> 

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

+0

您是否在执行操作后重新绑定了您的viewdata?另外,你是否设置RadioButton Helper的isChecked参数? – 2009-02-14 09:51:53

回答

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

这有帮助吗?