0

Coming from PHP background, I often used this:

How to POST data as an indexed array of arrays (without specifying indexes)

This way I could have item id's in the post data as array keys. Loop through the array and insert into DB the id's using the keys.

Now I'm trying to achieve the same in C# but cannot find a way. I tried searching for answers but nothing seems to suggest C# solution:

Get posted value In C# ASP.NET

Submitting form elements with the same name

I can't even get to the POST part. C# complains about this already:

<asp:TextBox ID="txt[1][1]" runat="server" />
<asp:TextBox ID="txt[1][2]" runat="server" />
<asp:TextBox ID="txt[1][3]" runat="server" />

Parser Error Message: 'txt[1][1]' is not a valid identifier.

Is there a better way in C# for me to do this?

Alejandro Galera
  • 3,445
  • 3
  • 24
  • 42
Shaakir
  • 464
  • 5
  • 13
  • special characters aren't allowed in variable names `[` and `]` – fubo May 18 '16 at 07:25
  • the POST data will consist of name-value-pairs based on the `name` attribute of form elements, not the `ID` – David Hedlund May 18 '16 at 07:26
  • You should change your `ID` to something like: `txt_1_1` instead of using `[` and `]` – Ian May 18 '16 at 07:29
  • Thanks David. I assumed that C# will give it the name attribut ethe same when i use ID. I changed the "ID" to "NAME" and managed to post data, It now complains about my loop. var postedValues = Request.Form.GetValues("txt"); foreach (var value in postedValues) { Response.Write(value); } Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. – Shaakir May 18 '16 at 07:30
  • @Ian, I thought of that, but then I would have to split the name to get id's ?? The numbers for ID / NAME of the field is not just to give it uniqueness but also holds item id's that i need. I like the way PHP automatically put it into array you can loop through. Can C# do this, or am i thinking about it wrong, Is there a better way? – Shaakir May 18 '16 at 07:34

0 Answers0