从 html 获取值到控制器

发布于 2024-10-10 15:32:47 字数 728 浏览 0 评论 0原文

我正在尝试从我的控制器访问用户在表中引入的值。

该表不是模型的一部分,查看源代码类似于:

<table id="tableSeriales" summary="Seriales" class="servicesT" cellspacing="0" style="width: 100%">
    <tr>
        <td class="servHd">Seriales</td>
    </tr>
    <tr id="t0">
        <td class="servBodL">
            <input id="0" type="text" value="1234" onkeypress = "return handleKeyPress(event, this.id);"/>
            <input id="1" type="text" value="578" onkeypress = "return handleKeyPress(event, this.id);"/>
            .
            .
            .
        </td>
    </tr>
</table>

如何从控制器获取这些值(1234, 578)?

接收表单集合不起作用,因为它没有获取表格...

谢谢。

I'm trying to access the values a user introduces in a table from my controller.

This table is NOT part of the model, and the view source code is something like:

<table id="tableSeriales" summary="Seriales" class="servicesT" cellspacing="0" style="width: 100%">
    <tr>
        <td class="servHd">Seriales</td>
    </tr>
    <tr id="t0">
        <td class="servBodL">
            <input id="0" type="text" value="1234" onkeypress = "return handleKeyPress(event, this.id);"/>
            <input id="1" type="text" value="578" onkeypress = "return handleKeyPress(event, this.id);"/>
            .
            .
            .
        </td>
    </tr>
</table>

How can I get those values (1234, 578) from the controller?

Receiving a formcollection doesn't work since it does not get the table...

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫瑾少女 2024-10-17 15:32:48

使用 FormCollection 应该可以工作,除非你的表格不在

标签内。


在 Lazarus 的评论之上,你可以尝试这个,但你必须设置 <每个属性的 code>name :

<input id="seriales[0]" name="seriales[0]" type="text" value="1234" onkeypress="return handleKeyPress(event, this.id);"/>
<input id="seriales[1]" name="seriales[1]" type="text" value="578" onkeypress="return handleKeyPress(event, this.id);"/>

现在,在您的 Action 方法中,您可以使您的方法如下所示:

[HttpPost]
public ActionResult MyMethod(IList<int> seriales)
{
    // seriales.Count() == 2
    // seriales[0] == 1234
    // seriales[1] == 578
    return View();
}

并且 seriales 将连接到这些值。

Using the FormCollection should work unless your table is not inside of a <form> tag


On top of Lazarus's comment, you can try this, but you have to set the name attribute for each:

<input id="seriales[0]" name="seriales[0]" type="text" value="1234" onkeypress="return handleKeyPress(event, this.id);"/>
<input id="seriales[1]" name="seriales[1]" type="text" value="578" onkeypress="return handleKeyPress(event, this.id);"/>

Now in your Action method you can make your method look like this:

[HttpPost]
public ActionResult MyMethod(IList<int> seriales)
{
    // seriales.Count() == 2
    // seriales[0] == 1234
    // seriales[1] == 578
    return View();
}

and seriales will be wired up to those values.

勿忘心安 2024-10-17 15:32:48

第一个选项:
使用 FormCollection 是访问动态数据的最简单方法。奇怪的是你无法从中获取这些值,你可以检查以下内容吗?

  1. 桌子里面是
    元素?
  2. 可以添加 name 属性吗
    到输入元素?注意
    表单项由其名称绑定,
    不是 ID。

第二个选项:
第二个选项是在模型中添加一个集合,并相应地命名所有内容。即

public class MyModel
{
  ...
  public IList<string> MyTableItems { get; set; }
}

在您看来使用以下名称:

<input name="MyTableItems[]" value="" />

First Option:
Using FormCollection is the simplest way to access dynamic data. It is strange that you cannot get those values from it, can you check the following?

  1. Is the table inside the
    element?
  2. Can you add name attribute
    to the input elements? Note that
    form items are bound by their names,
    not id.

Second Option:
The second option is to add a collection in your model, and name everything accordingly. i.e.

public class MyModel
{
  ...
  public IList<string> MyTableItems { get; set; }
}

and in your view use following names:

<input name="MyTableItems[]" value="" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文