开发者

Getting values from the html to the controller

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.


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.


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="" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜