Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何检索表单列表?_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# 如何检索表单列表?

C# 如何检索表单列表?,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我带着一张清单,上面有来自我数据库的信息。这以用户可以编辑字段的形式显示。我的意图是,当他们单击save按钮时,为他显示的这个列表将作为参数传递给一个方法。我迷路了,怎么了 看法 在这些情况下,应该发布某些列表的哪些值,可以在不绑定模型的情况下从表单中手动获取值 在您的问题中,我认为您的Bebida类中有一个唯一标识符,如Bebida.Id,因此示例代码可能是: 这样: @model Teste.Models.Produto @{ Layout = null; } @using (Ht

我带着一张清单,上面有来自我数据库的信息。这以用户可以编辑字段的形式显示。我的意图是,当他们单击save按钮时,为他显示的这个列表将作为参数传递给一个方法。我迷路了,怎么了

看法


在这些情况下,应该发布某些列表的哪些值,可以在不绑定模型的情况下从表单中手动获取值

在您的问题中,我认为您的Bebida类中有一个唯一标识符,如Bebida.Id,因此示例代码可能是: 这样:

@model Teste.Models.Produto

@{
    Layout = null;
}
@using (Html.BeginForm("Salvar", "ListaProdutoCab", FormMethod.Post))
{
    <table>
        <thead>
            <tr>
                <th>Selecione</th>
                <th>Produto</th>
                <th>Valor</th>

            </tr>
        </thead> @foreach (var item in Model.listaBebida)
        {
            <tr>
                <td> <input type='checkbox' name='chk_@item.Id' /> </td>
                <td> <input type='text' name='produto_@item.Id' /> </td>
                <td> <input type='text' name='valor_@item.Id' /> </td>
            </tr>
        }
    </table>
    <div id="modal">
        <div id="botoes">
            <button type="submit">Salvar</button>
        </div>
    </div>
    }
在你的行动中:

           [HttpPost]
            public ActionResult Salvar()
            {
              var checkboxkeys = Request.Form.AllKeys.Where(k=>k.Contains("chk_"));
              var CheckedCheckboxesId =checkboxkeys.Select(k=> Convert.ToInt32( k.Substring(4,k.Length-4)));

              var produtokeys = Request.Form.AllKeys.Where(k=>k.Contains("produto_"));
              var produtos= new Dictionary<int,string>();
              foreach(var item in produtokeys )
              {
                  produtos.Add(Convert.ToInt32( item.Substring(8,k.Length-8)),Request.Form[item]);
              }


              var valorkeys = Request.Form.AllKeys.Where(k=>k.Contains("valor_"));
              var valors= new Dictionary<int,string>();
              foreach(var item in valorkeys )
              {
                  valors.Add(Convert.ToInt32( item.Substring(6,k.Length-6)),Request.Form[item]);
              }

              // in here you have CheckedCheckboxesId, produtos and valors and you may want store them.

              return RedirectToAction("List");
            }
使用foreach循环会创建重复的id属性无效的html和重复的名称属性,这些属性与您的模型没有关系,因此在提交时无法绑定到您的模型。将其更改为for循环

例如,这将生成正确的名称属性

<input type="text" name="listaBebida[0].valor" ... />
<input type="text" name="listaBebida[1].valor" ... />
<input type="text" name="listaBebida[2].valor" ... />

旁注:您已经包含了produto的disabled属性,这意味着当您提交禁用的表单控件时,该属性在模型中将为null,而不会回发值。如果需要绑定,则将其设置为只读

将产品值恢复为null,就好像未加载对象,但已将其加载到方法中:carregarlistaproduto您的任何操作都没有任何参数,那么如何检索用户输入的值?通常情况下,您需要创建一个类似的模型类,但需要将它们传回。这是一个强类型的Produto,但我尝试使用“save”方法:public ActionResult save Product item{service.Salvar item.listaBebida,User;RedirectToAction return List;}将my product对象作为参数传递并获取相同的属性,但该对象仍返回null。这就是为什么我要在其中使用一个对象,它是一个其他对象的列表?我们将显示另一个对象的列表?下面注释中的代码是一个很好的开始。但是,您必须确保在视图中记录列表。这解释起来有点奇怪。您知道如何将字符串或int之类的值传递给视图并显示它。为了将其取回,视图需要使用它,而POST方法必须从模型中请求取回它。列表很棘手,因为您必须使用列表中的所有值。这是一个很大的疑问,因为我要求将此列表返回给我的控制器?更像是一个列表。。。。有没有办法让这个对象完全进入控制器?我在以后的服务中处理它?
public class Bebida
{
    //Chamadas externas
    public String nome{ get; set; }

    public Double Valor{ get; set; }

    public Bebida()
    {

    }
}
@model Teste.Models.Produto

@{
    Layout = null;
}
@using (Html.BeginForm("Salvar", "ListaProdutoCab", FormMethod.Post))
{
    <table>
        <thead>
            <tr>
                <th>Selecione</th>
                <th>Produto</th>
                <th>Valor</th>

            </tr>
        </thead> @foreach (var item in Model.listaBebida)
        {
            <tr>
                <td> <input type='checkbox' name='chk_@item.Id' /> </td>
                <td> <input type='text' name='produto_@item.Id' /> </td>
                <td> <input type='text' name='valor_@item.Id' /> </td>
            </tr>
        }
    </table>
    <div id="modal">
        <div id="botoes">
            <button type="submit">Salvar</button>
        </div>
    </div>
    }
           [HttpPost]
            public ActionResult Salvar()
            {
              var checkboxkeys = Request.Form.AllKeys.Where(k=>k.Contains("chk_"));
              var CheckedCheckboxesId =checkboxkeys.Select(k=> Convert.ToInt32( k.Substring(4,k.Length-4)));

              var produtokeys = Request.Form.AllKeys.Where(k=>k.Contains("produto_"));
              var produtos= new Dictionary<int,string>();
              foreach(var item in produtokeys )
              {
                  produtos.Add(Convert.ToInt32( item.Substring(8,k.Length-8)),Request.Form[item]);
              }


              var valorkeys = Request.Form.AllKeys.Where(k=>k.Contains("valor_"));
              var valors= new Dictionary<int,string>();
              foreach(var item in valorkeys )
              {
                  valors.Add(Convert.ToInt32( item.Substring(6,k.Length-6)),Request.Form[item]);
              }

              // in here you have CheckedCheckboxesId, produtos and valors and you may want store them.

              return RedirectToAction("List");
            }
@for(int i = 0; i < Model.listaBebida.Count, i++)
{
  <tr>
    <td>@Html.CheckBoxFor(m => m.listaBebida[i].isEditado)</td>
    <td>@Html.TextBoxFor(m => m.listaBebida[i].produto)</td>
    <td>@Html.TextBoxFor(m => m.listaBebida[i].valor) </td>
  </tr>
}
<input type="text" name="listaBebida[0].valor" ... />
<input type="text" name="listaBebida[1].valor" ... />
<input type="text" name="listaBebida[2].valor" ... />