gpt4 book ai didi

asp.net - MVC 3 在 IEnumerable 模型 View 中编辑数据

转载 作者:太空狗 更新时间:2023-10-29 13:46:48 25 4
gpt4 key购买 nike

我正在尝试在强类型 Razor View 中编辑项目列表。模板没有给我在单个 View 中编辑对象列表的选项,因此我将 ListView 与编辑 View 合并。我只需要在复选框中编辑一个 bool 字段。问题是我无法将数据传回 Controller 。我该怎么做? 表单集合查看数据?提前致谢。

代码如下:

模型:

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

Controller 操作:

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

最佳答案

How do i do it? FormCollection? Viewdata?

以上都不是,使用 View 模型:

[HttpPost]
public ActionResult EditPermissoes(IEnumerable<Permissao> model)
{
// loop through the model and for each item .HasPermissao will contain what you need
}

在您的 View 中使用编辑器模板而不是编写一些循环:

<table>
<tr>
<th></th>
<th>
Indicador
</th>
<th>
Nome
</th>
<th>Descrição</th>
<th></th>
</tr>
@Html.EditorForModel()
</table>

在相应的编辑器模板中(~/Views/Shared/EditorTemplates/Permissao.cshtml):

@model Permissao
<tr>
<td>
@Html.CheckBoxFor(x => x.HasPermissao)
</td>
<td>
@Html.DisplayFor(x => x.TipoP.IndID)
</td>
<td>
@Html.DisplayFor(x => x.TipoP.Nome)
</td>
<td>
@Html.DisplayFor(x => x.TipoP.Descricao)
</td>
</tr>

关于asp.net - MVC 3 在 IEnumerable 模型 View 中编辑数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7010038/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com