How to create dynamic list in Aspnet mvc4 using radioButton?

39 Views Asked by At

My Model is:

namespace MvcApplication5.Models
{
    public class DepartmentModelClass
    {
        public int Id { get; set; }
        public string Text1 { get; set; }
        public string Text2 { get; set; }
        public bool? Option1 { get; set; }
        public bool Option2 { get; set; }
    }
}

My Controller is:

 public ActionResult Index()
        {
            List<DepartmentModelClass> list = new List<DepartmentModelClass>();
            list.Add(new DepartmentModelClass { Id = 1, Text1 = "test1", Text2 = "test2", Option1 = false, Option2 = false });
            list.Add(new DepartmentModelClass { Id = 2, Text1 = "test3", Text2 = "test4", Option1 = false, Option2 = false });
            list.Add(new DepartmentModelClass { Id = 3, Text1 = "test5", Text2 = "test6", Option1 = false, Option2 = false });

            return View(list);
        }

My View Is:

@model IEnumerable<MvcApplication5.Models.DepartmentModelClass>

@{
    Layout = null;
}

@using (Html.BeginForm("Index", "Employee", FormMethod.Post, new { id = "idForm" })) {
    int i = 0;
    foreach (var item in Model) {
            <div class="row">
                <div class="form-group">
                    <span>@i:</span>
                    <input type="text" id="idtext1" [email protected] name="[@i].Text1" />
                    <input type="text" id="idtext2" [email protected] name="[@i].Text2" />
                    <hr />
                </div>
            </div>
        i += 1;
    }

    <input type="submit" value="Submit" />
}

My Screen is:

enter image description here

My Debug Is:

enter image description here

But, I would like to do same thing using RadioButtton:

<input type="radio" id="radio3" value="@item.Option1" name="[@i].Option1 == checked ? true : false" />

I want to set value TRUE or FALSE according my list item.

Can you help me? Thanks!

1

There are 1 best solutions below

1
sambeet das On

Can you please elaborate further.

Following is the code, if you want to display radio button based on the option1 value.

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "idForm" }))
    {
        int i = 0;
        foreach (var item in Model)
        {
            <div class="row">
                <div class="form-group">
                    <span>@i:</span>
                    @if (item.Option1 == true)
                    {
                        <input type="radio" id="radio_@i" value="@item.Option1" name="radio_@i" checked="checked" />
                        <input type="text" id="idtext1" [email protected] name="[@i].Text1" />
                        <input type="text" id="idtext2" [email protected] name="[@i].Text2" />
                    }
                    else
                    {
                        <input type="radio" id="radio_@i" value="@item.Option1" name="radio_@i" />
                        <input type="text" id="idtext1" [email protected] name="[@i].Text1" disabled="disabled" />
                        <input type="text" id="idtext2" [email protected] name="[@i].Text2" disabled="disabled" />
                    }


                    <hr />
                </div>
            </div>
            i += 1;
        }

        <input type="submit" value="Submit" />
    }