user1404623
user1404623

Reputation:

Creating a list of radio buttons

I m working with asp.net mvc3 to create a list of radiobuttons. I need to get a list from the controller and display it in the view. For each corresponding list in the view I have YES/NO radio button and need to generate a string at the end saying for each item in the list if the radiobutton is yes then 1 else 0.

So for example if I have 10 items in the list then I need to display them in the view (default values of the items being false) and then generate a string on submit where each character in the string corresponds to the bool value of each item in the list.

Can anyone give me an idea how do I do this in mvc3 ? Thanks for the help in advance.

UPDATE

Here is the code I am trying :

My class has two properties :

public List<Module> lstModules { get; set; } // gives the list of items

public List<bool> IsModuleActivelst { get; set; } //gives the bool values 

Here in controller I need to create a list and the corresponding bool values for that. I am stuck here and not able to generate the code. Anyways I ll explain the pseudocode

public class MMController : Controller
{
    [HttpGet]
    public ActionResult Clients()
    {
        //I need to generate the list - using lstModules prop

        // Assign the list with the predefined values and if not just give the default values- using IsModuleActivelst  prop

     }
}

Here I create the view :

 @foreach (var i in Model.lstModules)
{
    <div class="formlabel">
        <div align="right" class="label">

            @Html.LabelFor(model => model.lstModules):</div>
    </div>

    <div class="formelement">
     <label for="radioYes" class="visible-enable" style="cursor:pointer;position:relative;">
                @Html.RadioButtonFor(model => model.IsModuleActivelst, "True", new { @id = "radioYes", @style = "display:none;" })
               <span>Yes</span></label>
                <label for="radioNo" class="visible-disable" style="cursor:pointer;position:relative;">
                @Html.RadioButtonFor(model => model.IsModuleActivelst, "False", new { @id = "radioNo", @style = "display:none;" })
                 <span>No</span></label>
    </div>
}

Upvotes: 0

Views: 1742

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038810

I would recommend you to start by defining a view model that will represent the information that you need to be working with in the particular view. So far you have mentioned a list of modules where the user needs to set whether a module is active or not using a radio button (personally I would use a checkbox for True/False status but that's your own decision):

public class ModuleViewModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool IsActive { get; set; }
}

public class MyViewModel
{
    public IEnumerable<ModuleViewModel> Modules { get; set; }
}

Then you could define a controller that will populate the view model, render the form and have another action to handle the form submission:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            // TODO: this information could come from a database or something
            Modules = new[]
            {
                new ModuleViewModel { Id = 1, Name = "module 1", IsActive = true },
                new ModuleViewModel { Id = 2, Name = "module 2", IsActive = true },
                new ModuleViewModel { Id = 3, Name = "module 3", IsActive = false },
            }
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        return Content(
            string.Format(
                "Thank you for selecting the following values: {0}",
                string.Join(" ", model.Modules.Select(x => string.Format("model id: {0}, active: {1}", x.Id, x.IsActive)))
            )
        );
    }
}

The last part is to define the view (~/Views/Home/Index.cshtml):

@model MyViewModel

@using (Html.BeginForm())
{
    @Html.EditorFor(x => x.Modules)
    <button type="submit">OK</button>
}

And finally the corresponding editor template that will automatically be rendered for each element of the Modules collection - notice that the name and location of the template is important - ~/Views/Shared/EditorTemplates/ModuleViewModel.cshtml:

@model ModuleViewModel

<div>
    @Html.HiddenFor(x => x.Id)
    @Html.HiddenFor(x => x.Name)

    <h2>@Html.DisplayFor(x => x.Name)</h2>
    @Html.Label("IsActiveTrue", "Yes")
    @Html.RadioButtonFor(x => x.IsActive, "True", new { id = Html.ViewData.TemplateInfo.GetFullHtmlFieldId("IsActiveTrue") })
    <br/>
    @Html.Label("IsActiveFalse", "No")
    @Html.RadioButtonFor(x => x.IsActive, "False", new { id = Html.ViewData.TemplateInfo.GetFullHtmlFieldId("IsActiveFalse") })
</div>

Upvotes: 0

Related Questions