0

我的模型是:

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

我的控制器是:

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

我的观点是:

@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" value=@item.Text1 name="[@i].Text1" />
                    <input type="text" id="idtext2" value=@item.Text2 name="[@i].Text2" />
                    <hr />
                </div>
            </div>
        i += 1;
    }

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

我的屏幕是:

在此处输入图像描述

我的调试是:

在此处输入图像描述

但是,我想使用 RadioButton 做同样的事情:

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

我想根据我的列表项设置值 TRUE 或 FALSE。

你能帮助我吗?谢谢!

4

1 回答 1

0

您能否进一步详细说明。

以下是代码,如果要根据 option1 值显示单选按钮。

@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" value=@item.Text1 name="[@i].Text1" />
                        <input type="text" id="idtext2" value=@item.Text2 name="[@i].Text2" />
                    }
                    else
                    {
                        <input type="radio" id="radio_@i" value="@item.Option1" name="radio_@i" />
                        <input type="text" id="idtext1" value=@item.Text1 name="[@i].Text1" disabled="disabled" />
                        <input type="text" id="idtext2" value=@item.Text2 name="[@i].Text2" disabled="disabled" />
                    }


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

        <input type="submit" value="Submit" />
    }
于 2020-10-18T13:04:14.920 回答