Default modelbinding for enum in ASP.NET MVC?

How do I bind the value of a radio button generated with an Html.RadioButton()

HTML helper to a field that is structured as a type?

Less abstract:

CommonProject.Services.SearchBag.Effects:

public enum Effects
{
    Any,
    Solid,
    Effect
}

      

In strongly typed ViewData:

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

      

And in my opinion (it doesn't actually work):

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

      

UPDATE
It seems to work once ..
Initially it creates radiobjects at will, then when you change the value and send back the value is linked correctly. Then, when you restore the page, all the button values ​​will be set to the value you selected earlier.

0


source to share


1 answer


If your view is strongly typed with SearchBag as your Data Data class, you should do something along these lines:

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

      

Then, when your submission form is submitted back to the controller, it will look something like this:



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

      

Does it help?

+1


source







All Articles