【问题标题】:MVC 3 Edit data in a IEnumerable Model ViewMVC 3 在 IEnumerable 模型视图中编辑数据
【发布时间】: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


【解决方案1】:

我该怎么做?表单集合?查看数据?

以上都不是,使用视图模型:

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

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

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

【讨论】:

  • 是的!这很好用,将有助于未来的编码!非常感谢!
  • @Darin Dimitrov 我已经重复了你的指示,我有一个奇怪的例外,像这样传递到字典中的模型项的类型是'System.Collections.Generic.List`1[DomainModel.Models. MoneyExchangeMes​​sage]',但此字典需要“DomainModel.Models.MoneyExchangeMes​​sage”类型的模型项。
  • @IamStalker,我猜你将错误的类型传递给视图,或者视图被强类型化为错误的类型。
  • 抱歉,不是,我已经检查过了,如果你想我会提出问题,如果你有时间看的话?
  • 当然,您可以开始一个新问题。
猜你喜欢
  • 1970-01-01
  • 2011-08-14
  • 1970-01-01
  • 2013-09-13
  • 2016-04-12
  • 2014-02-12
  • 1970-01-01
  • 1970-01-01
  • 2011-05-13
相关资源
最近更新 更多