【发布时间】:2011-10-24 00:20:02
【问题描述】:
我正在尝试在强类型剃刀视图中编辑项目列表。模板没有让我选择在单个视图中编辑对象列表,因此我将列表视图与编辑视图合并。我只需要在复选框中编辑一个布尔字段。
问题是我无法将数据返回给控制器。我该怎么做? FormCollection? Viewdata?提前致谢。
代码如下:
型号:
public class Permissao
{
public int ID { get; set; }
public TipoPermissao TipoP { get; set; }
public bool HasPermissao { get; set; }
public string UtilizadorID { get; set; }
}
public class TipoPermissao
{
public int ID { get; set; }
public string Nome { get; set; }
public string Descricao { get; set; }
public int IndID { get; set; }
}
控制器操作:
public ActionResult EditPermissoes(string id)
{
return View(db.Permissoes.Include("TipoP").Where(p => p.UtilizadorID == id));
}
[HttpPost]
public ActionResult EditPermissoes(FormCollection collection)
{
//TODO: Get data from view
return RedirectToAction("GerirUtilizadores");
}
查看:
@model IEnumerable<MIQ.Models.Permissao>
@{
ViewBag.Title = "EditPermissoes";
}
@using (Html.BeginForm())
{
<table>
<tr>
<th></th>
<th>
Indicador
</th>
<th>
Nome
</th>
<th>Descrição</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.CheckBoxFor(modelItem => item.HasPermissao)
</td>
<td>
@Html.DisplayFor(modelItem => item.TipoP.IndID)
</td>
<td>
@Html.DisplayFor(modelItem => item.TipoP.Nome)
</td>
<td>
@Html.DisplayFor(modelItem => item.TipoP.Descricao)
</td>
</tr>
}
</table>
<p>
<input type="submit" value="Guardar" />
</p>
}
【问题讨论】:
-
它根本没有回到FormCollection吗?
-
我认为是,只是不知道如何从 FormCollection 中获取它。
-
我这样做:var value = collection["item.HasPermissao"];
-
我得到了这个:“假,真,假,假,真,假,假”,只有 5 个项目。它为每个真值添加了一个额外的假!?为什么?
标签: asp.net html asp.net-mvc razor