0

Seeking help for coding ASP.... I dont know ASP, but somehow need to follow up the current project, hope someone can help me for that....

I have a form with a dropdown list, client would like to change it to radio button. I find in a .cshtml file have code as below should be useful:

@Html.DropDownListFor(m => m.selectedBSFlag, Model.bsFlag, new { @class =  "form-control input-sm", ng_model = "inputForm.bsFlag" })

Any ideas how to make the changes? Do let me know if any more info need. Thanks.

Best regards, Sky

Andre Pena
  • 56,650
  • 48
  • 196
  • 243
Sky
  • 11
  • 1
  • In order to give a good answer, you need to show your models - what is the property you want to bind the selected value to, what is the property containing the collection that displays the options to select from, and what are the properties of the model in that collection? –  Jul 29 '15 at 04:01

1 Answers1

0

We wouldn't call this ASP, we would call it ASP.NET MVC.

You should check the RadioButtonFor HTML extension. This question provides a good example.

Copy, for readability

    public class PropMgmtViewModel
{
    public Property Property { get; set; }
    public IEnumerable<Property> Properties { get; set; }
    public SelectList Cities { get; private set; }


    static Dictionary<int, string> CitiesDict = new Dictionary<int, string>()
{
    { 1 ,"Chicago"},
    { 2 ,"New York"},
    { 3 ,"Zimbabwe"},
};
    public PropMgmtViewModel()
    {
        Cities = new SelectList(CitiesDict, "Key", "Value");
    }

View code:

 @foreach (var radioitem in Model.Cities)
    {
        @radioitem.Text;
    @Html.RadioButtonFor(model => model.Property.City, radioitem.Value);
    }
        @Html.DropDownListFor(model => model.Property.City, Model.Cities,"Seciniz") 
Community
  • 1
  • 1
Andre Pena
  • 56,650
  • 48
  • 196
  • 243
  • No need to go to the unnecessary extra overhead of creating a `SelectList` from the `Dictionary` –  Jul 29 '15 at 04:19
  • I would recommend this answer for the people who arrived here via google: https://stackoverflow.com/a/16176153/4378740 – chakeda Feb 23 '18 at 19:35