如何解决如何使用radioButton在Aspnet Mvc4中创建动态列表?
我的模特是:
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",Option2 = false });
list.Add(new DepartmentModelClass { Id = 3,Text1 = "test5",Text2 = "test6",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" />
}
我的屏幕是:
我的调试是:
但是,我想使用RadioButtton做同样的事情:
<input type="radio" id="radio3" value="@item.Option1" name="[@i].Option1 == checked ? true : false" />
我想根据我的列表项将值设置为TRUE或FALSE。
你能帮我吗?谢谢!
解决方法
请进一步详细说明。
以下是代码,如果要基于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" />
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。