使用类型文本请求输入字段的ID

时间:2013-12-11 13:03:07

标签: 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"/>
}

控制器中的代码

    public ActionResult bestelProducten()
    {
        string[] values = Request.Form.GetValues("articles");

        foreach (String s in values)
        {
            DialogResult d = TopMostMessageBox.Show(s, "Test", MessageBoxButtons.OK);
        }       

        return View();
    }

让我们说我的articleList中有三篇文章。它将生成三个输入框。我在那些文本框中输入1,2,3。对话框结果将显示输入框的值(1,2,3)。但我需要该文本框的ID与我得到的每个值。例如,值为1,我需要该文本框的id(id)articlenumber,值为2,我需要该文本框的(id)文章编号。

有人可以帮助我吗?

3 个答案:

答案 0 :(得分:1)

试试这个

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

Demo

答案 1 :(得分:0)

据我所知,您希望获得在c#代码中提交给服务器的表单元素的ID。我不认为这是可能的(服务器不会提交ID),但通过一些更改,您将能够获得您正在寻找的结果。

我必须做出一些假设 - 如果假设不太正确,我可以随意评论这一点,我会尽量适应。

首先,我将假设您的变量“productList”是具有两个属性“articleNumber”和“article”的类型的IList<>。我们称这种类型为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文件执行以下操作:

@for (int i; i < productList.Count; i++)
{
  <input name="[i].articleNumber" type="hidden" value="@item.articleNumber" />
  <input name="[i].article" type="text"/>
}

我使用隐藏字段,而不是使用ID。这样,文章编号将随文章一起提交。 [i]参数告诉MVC对象是列表的一部分,articleNumberarticle是同一对象的一部分,从而将数字和文章保持在一起,假设接收对象有这些字段(我们假设它)。有关模型绑定到列表的更多信息,请参阅this article by Phill Haack

您的控制器操作将变为:

public ActionResult bestelProducten(IList<Article> articles)
{
    foreach (Article a in articles)
    {
        DialogResult d = TopMostMessageBox.Show(string.Format("Article Number: {0} - Article Order: {1}", a.articleNumber, a.article), "Test", MessageBoxButtons.OK);
    }       

    return View();
}

控制器中的articles参数将自动绑定到您的命名[i].参数列表。

我已经做到了这一点,所以可能需要一些调整 - 但我希望这就是你要找的。


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

其次,希望我现在可以更全面地帮助你!请注意,在上面的代码中,您可以IList替换ListIList是实现List的接口。我现在会继续使用List

我现在看到文本框是什么。所以,你的Article实际上有三个字段,第三个整数值,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; }
}

2)在你的视图中,像这样设置你的for循环(假设你正在使用productList,正如你最初所说的那样):

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

  <!-- As you are submitting to the model, you need to create form 
  elements that will submit to it. The first line provides the article 
  number, the second is your textbox --> 
  <input name="[i].articleNumber" type="hidden" value="@item.articleNumber" />
  <input name="[i].articleOrder" type="text"/>
}

3)像这样设置你的控制器:

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

我为你的控制器动作使用了一个单独的模型,因为我得到的印象是你只想显示文章文本,而你想要一篇文章订单。我选择的模型只是获取您需要的数据。


如果您想在同一页面上编辑文章文字,请将控制器中的参数更改为Article,然后在您的视图中执行此操作:

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

在这种情况下,您无需担心ArticleOrderModel

答案 2 :(得分:0)

@DanielNaylor productList属于List类型我现在不知道与IList有多远,我只是在10周前盯着c#asp.net! Article类型的对象将放在该列表中(articleList List<Article>)此视图具有Article类型的模型。我在foreach循环中使用html助手来显示该列表中的文章,并自己添加一个输入类型,如代码所示。我将所有文章与输入字段一起写在表格中。他们应该能够在输入字段中输入金额。我需要每个产品上的输入字段的值和文章中的文章编号,所以我可以按照这些文章的顺序将其转换