1

Please refer to this link

http://aspsolution.net/Code/5/5215/How-to-loop-through-ViewBag-List-in-Jquery/#

I followed what is written in the website. However the Json.Result doesn't appear. I tried to include this one (@using Newtonsoft.Json) in the html but it's still not appearing. Why is that? Is there any solution for this?

This is my script:

fillDatatable();

function fillDatatable() {
    var cart = @Html.Raw(Json.Result(@ViewBag.cart));
    alert(cart.Items.ItemID);
}

Edited:

if (PurchaseOrder != "") {
    var tableHTML = '';
    $.each(PurchaseOrder, function (index, value) {
        tableHTML += "<tr>";
        tableHTML += "<td>" + value.items.itemID + "</td>";
        tableHTML += "<td>" + value.items.itemModelDescription + "</td>";
        tableHTML += "<td>";
        tableHTML += "<input id='UnitPrice" + value.items.ItemID + "' class='form-control b-r-xl text-right' value='" + value.items.itemUnitPrice + "' oninput='return change_unitprice('" + value.items.itemID + "')' />";
        tableHTML += "</td>";
        tableHTML += "<td>";
        tableHTML += "  </tr>";
    });
    $("#TableRecords").html(tableHTML);
}

See my HTML inside the loop where you can see change_unitprice function. I'm trying to pass the value of an id there, but according to the result of console it is undefined? Why is that?

Jackdaw
  • 7,626
  • 5
  • 15
  • 33
  • You can refer to here: https://stackoverflow.com/questions/42795997/get-json-from-controller-mvc-in-java-script – Tan Thanh Apr 21 '21 at 05:56
  • You can refer to here: https://stackoverflow.com/questions/42795997/get-json-from-controller-mvc-in-java-script – Tan Thanh Apr 21 '21 at 05:57

2 Answers2

0

I think the simplest method that works for complex object would be like this:

@Html.Raw(Json.Encode(ViewBag.cart))
Adlorem
  • 1,457
  • 1
  • 11
  • 10
0

The example you are referencing does not contain the code you included to the question. It describes how to use Json.Encode() to obtain data from the ViewBag field. Possible problems why your code doesn't work are:

  1. Using not defined the cart field.
  2. Using the Items property that does not defined.
  3. Trying to work with HTML document that does not loaded yet.

Therefore, use function from this example as is:

<script>
    $(document).ready(function(){
        var StockList =@Html.Raw(Json.Encode(@ViewBag.StockList));
        if (StockList != '') {
            var tableHtml;
            $.each(StockList, function( index, value ) {
                tableHtml +="<tr><td>" + value.stockId + "</td><td>" + value.StockName + "</td><td>" + value.StockPrice + "</td></tr>";
              });
            $("#output").html(tableHtml);
        }
    })
</script>

If it does not work (probably because of the jquery disable by Layout = null; line in the Index.cshtml ) add the following line after the <head> tag:

<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.6.2.min.js"></script>

For additional information see the following post: https://stackoverflow.com/a/10779161/6630084

Jackdaw
  • 7,626
  • 5
  • 15
  • 33
  • It totall worked! But please see my edited comment. Where am I wrong? Is there anything wrong with concatenation ? – Jeffrey Estrera Apr 22 '21 at 00:07
  • @Jeffrey Estrera: Can you add full code of the `Index.cshtml`, the `Index()` action and your model definition, please? It seems a bug in code that we don't have it. – Jackdaw Apr 22 '21 at 06:42