C# 类型为text的输入字段的请求id

C# 类型为text的输入字段的请求id,c#,asp.net-mvc-4,visual-studio-2012,C#,Asp.net Mvc 4,Visual Studio 2012,我需要获取动态生成的文本框的id。使用c#、ASP.NET、VisualStudio 2012 MVC 4 cshtml文件中的代码: @foreach (var item in productList) { <input id="@item.articleNumber" name="articles" type="text"/> } @for (int i; i < productList.Count; i++) { <input name="[i].arti

我需要获取动态生成的文本框的id。使用c#、ASP.NET、VisualStudio 2012 MVC 4

cshtml文件中的代码:

@foreach (var item in productList)
{
  <input id="@item.articleNumber" name="articles" type="text"/>
}
@for (int i; i < productList.Count; i++)
{
  <input name="[i].articleNumber" type="hidden" value="@item.articleNumber" />
  <input name="[i].article" type="text"/>
}
假设我的文章列表中有三篇文章。它将生成三个输入框。我在这些文本框中输入1,2,3。对话框结果将显示输入框(1、2、3)的值。但是我需要文本框的id和我得到的每个值。例如,对于值1,我需要该文本框的id(id)文章编号;对于值2,我需要该文本框的(id)文章编号

有人能帮我吗?

试试这个

$(function(){
  $('[name="articles"]').each(function(){
     console.log($(this).attr("id"));
  });
});

据我所知,您希望获得在c#代码中提交给服务器的表单元素的ID。我认为这是不可能的(服务器不会提交ID),但是只要做一些更改,您就可以得到您想要的结果

不过,我必须做出一些假设——如果这些假设不太正确,请随意评论,我会尽量适应

首先,我假设变量“productList”是一个具有两个属性的
IList
,即“articleNumber”和“article”。我们将这种类型称为
文章
,其形式如下:

public class Article
{
  public string article { get; set; }

  // I'm assuming it's just a string here
  public string articleNumber { get; set; } 
}
我还假设这不是传递给此视图(如果有的话)的模型或视图模型的一部分,所以我不会在这里使用任何HtmlHelpers

可以对cshtml文件执行以下操作:

@foreach (var item in productList)
{
  <input id="@item.articleNumber" name="articles" type="text"/>
}
@for (int i; i < productList.Count; i++)
{
  <input name="[i].articleNumber" type="hidden" value="@item.articleNumber" />
  <input name="[i].article" type="text"/>
}
控制器中的
articles
参数将自动绑定到命名的
[i]列表。
参数

我已经做了我的头顶,所以它可能需要一些调整-但我希望这是你一直在寻找的


更新:首先,我在for循环中得到了两个错误的参数-修复了这个问题

第二,希望我现在能更充分地帮助你!注意,在上面的代码中,您可以将
IList
交换为
List
IList
是一个实现
List
的接口。不过现在我将继续使用
List

我知道文本框现在是什么了。因此,您的
文章实际上有三个字段,第三个整数值是articleOrder。在这种情况下,我会这样做:

1) 创建一个如下所示的
ArticleOrderModel
对象:

public class ArticleOrderModel
{
  // This is the number of your article
  public int ArticleNumber { get; set; }

  // This is the order it appears in
  public int ArticleOrder { get; set; }
}
public ActionResult bestelProducten(List<ArticleOrderModel> model)
{
    // Get your article list out of storage here

    foreach (ArticleOrderModel a in model)
    {
        // Here, I recommend that you put the order into the original Article objects,
        // I just will leave the original code here.
        DialogResult d = TopMostMessageBox.Show(string.Format("Article Number: {0} - Article: {1}", a.articleNumber, a.articleOrder), "Test", MessageBoxButtons.OK);
    }       

    return View();
}
2) 在您看来,按如下方式设置for循环(假设您使用的是productList,正如您最初提出的问题所述):


在这种情况下,您不必为
ArticleOrderModel

@DanielNaylor productList属于List类型而烦恼,我现在不知道这与我10周前刚开始使用c#asp.net时的IList有多大不同!类型文章的对象将进入该列表(articleList
list
)此视图具有类型文章的模型。我在foreach循环中使用html助手来显示该列表中的任何文章,并自己添加一个输入类型,如代码所示。我把每篇文章连同输入字段一起写在一个表格中。他们应该能够在输入字段中输入金额。我需要每个产品上输入字段的值以及同一篇文章中的文章编号,这样我就可以按照这些文章的顺序来打开它

我必须回答这个问题,因为我的评论不能放在这个框中。对不起,答案是给你的
public ActionResult bestelProducten(List<ArticleOrderModel> model)
{
    // Get your article list out of storage here

    foreach (ArticleOrderModel a in model)
    {
        // Here, I recommend that you put the order into the original Article objects,
        // I just will leave the original code here.
        DialogResult d = TopMostMessageBox.Show(string.Format("Article Number: {0} - Article: {1}", a.articleNumber, a.articleOrder), "Test", MessageBoxButtons.OK);
    }       

    return View();
}
@for (int i; i < productList.Count; i++)
{
  <!-- Add your HtmlHelpers here as you mention -->
  @Html.DisplayFor(x => x[i].articleNumber);
  @Html.HiddenFor(x => x[i].articleNumber);
  @Html.EditorFor(x => x[i].article);
  @Html.EditorFor(x => x[i].articleOrder);
}