0

an HTML table requires 2 cells of data from a JSON object that a default.aspx.cs file receives via POST. The task of parsing this JSON object is a little tricky as the data can vary in contents quite considerably. However, VS' immediate window confirms that the JsonResult[""] nomenclature is working fine (IOW, I am succesfully receiving data from a JSON object in a POST request). However, I get inconsistent performance when trying to implement the code below.

  if (Request.HttpMethod.ToString() == "POST") {

    if (Request.Form["Item"] != null) {
      Newtonsoft.Json.Linq.JObject JsonResult = (Newtonsoft.Json.Linq.JObject)JsonConvert.DeserializeObject(Request.Form["Item"]);

      if (JsonResult["requestType"] != null) {
        cartItem = new CartItem(
          "TestCode",
          JsonResult["containerType"].ToString(),
          JsonResult["resultType"].ToString(),
          1,
          3.99
          );

        // populate empty cells with data, place cells in row and place row in table.
        foreach (var item in cartItem.getCartCells().Select((value, index) => new { value, index })) {
          try {
            cartCells.Add(new TableCell());
            int i = cartCells.Count() - 1;
            cartCells[i].Text = (item.value != null || item.value != "") ? item.value : " - ";
            tr.Cells.Add(cartCells[i]);
          } catch (Exception ex) {
            Response.Write("shopping cart failed to run: " + ex.Message);
            break;
          }
        }
        shoppingCart.Rows.Add(tr);
      }
    }
  }

Previously, I had a stub performing a similar task without any problems. Now however, the additional row that this code is supposed to add to my table is not appearing on POST. Strangely, when I add Response.Write("Write a test") after checking the request method, the table builds just fine. Is there some kind of Async problem going on here? Intellisense and the debugger aren't picking up anything...

Jay Edwards
  • 950
  • 1
  • 12
  • 21

1 Answers1

0

The logic above is actually sound. The reason it wasn't working had to do with my submission. I was abstracting data from my form and submitting it with JS (as I actually need rather a lot more data than the form alone encompasses). What hadn't occured to me was that the button i was using was also submitting my form. The result was a messy header that, it appears, had been mashed together out of two requests. The solution was to replace <button onclick="Service.refresh()"></button> with <input type="button" onClick="Service.refresh()">...

as an aside, this thread may also be useful to people suffering similar headaches: <button> vs. <input type="button" />. Which to use?

Community
  • 1
  • 1
Jay Edwards
  • 950
  • 1
  • 12
  • 21