【问题标题】:Error in form collection MVC 4表单集合 MVC 4 中的错误
【发布时间】:2014-07-29 01:24:48
【问题描述】:

我正在尝试使用 formcollection 类将文本框值添加到 DB,但每次它只给出一个文本框值而不是其他文本框值。我正在分享我到目前为止所做的事情。请指导我

视图中的文本框:

<input type="text" name="abcd" autocomplete="off" id="PartNumber0" style="width:130px;" />
<input type="text" name="abcd" autocomplete="off" id="PartNumber1" style="width:130px;" />
<input type="text" name="abcd" autocomplete="off" id="PartNumber2" style="width:130px;" />
<input type="text" name="abcd" autocomplete="off" id="PartNumber3" style="width:130px;" />
<input type="text" name="abcd" autocomplete="off" id="PartNumber4" style="width:130px;" />
<input type="text" name="abcd" autocomplete="off" id="PartNumber5" style="width:130px;" />

动作方法:

public async Task<ActionResult> Create(FormCollection values)
        {    
                    var abc = values["abcd"].ToString();
                    var getID = await db.Parts.Where(c =>c.PartNumber==abc).FirstOrDefaultAsync();                            
                }

它只是将第一个文本框名称“abcd”值保存到 DB,但我想要 DB 中的所有文本框值,请指导我

【问题讨论】:

  • 您能在表单中显示另一个文本元素的视图 html 吗?
  • 其他文本框在哪里?
  • @VahidND 我已经更新了我的问题,请检查
  • @NeilThompson 我已经更新了我的问题,请检查

标签: asp.net asp.net-mvc linq entity-framework asp.net-mvc-4


【解决方案1】:

这是因为你使用的是同名 Form Collection 类读取控件的名称 使用以下代码,您将获得不同名称的文本框的不同值

@using (@Html.BeginForm("Demo", "Admin"))
{
<input type="text" name="PartNumber0" autocomplete="off" id="PartNumber0" style="width:130px;" />
<input type="text" name="PartNumber1" autocomplete="off" id="PartNumber1" style="width:130px;" />
<input type="text" name="PartNumber2" autocomplete="off" id="PartNumber2" style="width:130px;" />
<input type="text" name="PartNumber3" autocomplete="off" id="PartNumber3" style="width:130px;" />
<input type="text" name="PartNumber4" autocomplete="off" id="PartNumber4" style="width:130px;" />
<input type="text" name="PartNumber5" autocomplete="off" id="PartNumber5" style="width:130px;" />
<input type="submit" value="Click" />
}

谢谢

【讨论】:

  • 感谢您的帮助,但我如何在控制器操作方法中使用这些值?
  • 字符串 a = formvalues["PartNumber0"];字符串 b = formvalues["PartNumber1"];字符串 c = formvalues["PartNumber2"];字符串 d = formvalues["PartNumber3"];字符串 e = formvalues["PartNumber4"];这就是你可以检索任何表单集合值的方式
  • 它会起作用,但我建议使用 ViewModel 而不是 FormCollection。
猜你喜欢
  • 1970-01-01
  • 2013-06-18
  • 2015-09-02
  • 1970-01-01
  • 1970-01-01
  • 2013-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多