(我不是MVC专家,只是说)
我需要渲染一个包含4列的<table>
:
最后3个是布尔值,需要绑定到单选按钮,此单选按钮需要为每行分组。
我知道在视图中显示这种方法的方法很多,但不确定确保数据正确提交回Controller的最佳方法。
型号:
public class DetailModel
{
//this displays the user information
public Users_SelectByUserId User { get; set; }
//This is what builds the Table with the 4 columns.
public List<UserMinistryRef_SelectByUserID> MinistryRef { get; set; }
}
-
public class UserMinistryRef_SelectByUserID
{
public int UserRecordId { get; set; }
public int MinistryId { get; set; }
public bool Minadmin { get; set; }
public bool Updater { get; set; }
public bool Viewer { get; set; }
public string mname { get; set; }
}
显示的最佳方式是什么,并且能够正确地填充回控制器?我试图遍历MinistryRef集合,但我没有得到太多运气,在循环通过集合时无法使用Razor,并且绑定到第三方网格控件刚刚变得相当忙乱。
我不确定自己是否走在正确的轨道上......
@foreach (UserMinistryRef_SelectByUserID ministry in Model.MinistryRef)
{
<tr>
<td>@ministry.mname</td>
***NOte the below @Helpers are not supposed to work, just trying to show what I'm hoping to achieve.
<td>
@Html.RadioButtonFor(Model.MinistryRef.MinAdmin == true)
</td>
<td>
Html.RadioButtonFor(Model.MinistryRef.Viewer == true)
</td>
<td>
Html.RadioButtonFor(Model.MinistryRef.Viewer == false && Model.MinistryRef.MinAdmin == false)
</td>
</tr>
}
有什么想法吗?
答案 0 :(得分:1)
也许您可以使用int抽象来控制单选按钮组:
模型
public class UserMinistryRef_SelectByUserID
{
public int UserRecordId { get; set; }
public int MinistryId { get; set; }
public bool Minadmin { get; set; }
public bool Updater { get; set; }
public bool Viewer { get; set; }
public string mname { get; set; }
public int AccessRights { get; set; } //0 = viewer, 1 = updater, 2 = minadmin
}
视图
@{ int count = 0; }
@foreach (UserMinistryRef_SelectByUserID ministry in Model.MinistryRef)
{
<tr>
<td>@ministry.mname</td>
<td>
<input name="MinistryRef[@(count)].AccessRights" type="radio" value="0" @if(ministry.Viewer){<text>checked="checked"</text>} />
</td>
<td>
<input name="MinistryRef[@(count)].AccessRights" type="radio" value="1" @if(ministry.Updater){<text>checked="checked"</text>} />
</td>
<td>
<input name="MinistryRef[@(count)].AccessRights" type="radio" value="2" @if(ministry.Minadmin){<text>checked="checked"</text>} />
</td>
</tr>
count++;
}
控制器
[HttpPost]
public ActionResult action( DetailModel vm )
{
if (ModelState.IsValid)
{
foreach( var min in vm.MinistryRef )
{
switch( min.AccessRights )
{
case 0: /* Viewer */
case 1: /* Updater */
case 2: /* Minadmin */
}
}
}
return RedirectToAction("SomeHttpGet");
}