Pass (Send) List from Controller to View using jQuery AJAX in ASP.Net MVC

Last Reply 20 days ago By dharmendr

Posted 22 days ago

Dear Brother

I create progran (token number) and y now this use in counter

        public JsonResult btnPrintToken()
        {           
            if (Session["Tokenqueu"] == null)
            {
                Queue<int> queueToken = new Queue<int>();
                Session["Tokenqueu"] = queueToken;
            }
            List<int> listnumbertoken = new List<int>();
            Queue<int> tokenQueue = (Queue<int>)Session["Tokenqueu"];
            //string mesg = "There are" + tokenQueue.Count.ToString() + "Customer in the queue before you";
           if(Session["Lastnumbertoken"] == null)
            {
                Session["Lastnumbertoken"] =0;
            }
            int nextnumbertoken = (int)Session["Lastnumbertoken"] + 1;
            Session["Lastnumbertoken"] = nextnumbertoken;
            tokenQueue.Enqueue(nextnumbertoken);

            foreach(int token in tokenQueue)
            {               
                listnumbertoken.Add(token);
            }
            
            return Json(listnumbertoken, JsonRequestBehavior.AllowGet);
        }

 

    <script type="text/javascript">
        $(document).ready(function () {
            $('#btnPrintToken').click(function () {
                $.ajax({
                    url: '@Url.Action("btnPrintToken", "token")',
                    type: 'GET',
                    dataType: 'json',
                    success: function (N) {
                        $(N).each(function (x) {
                        listTokens.append($('<option/>', x));
                        });
                    },
                });
            });
        });
    </script>

how get this list intger in json and read in my list html

thanks

Posted 20 days ago Modified on 20 days ago

Hi Ah1970,

Check this example. Now please take its reference and correct your code.

View

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $('#btnPrintToken').click(function () {
            $.ajax({
                url: 'Home/btnPrintToken',
                type: 'POST',
                dataType: 'json',
                success: function (N) {
                    $('#listTokens').empty();
                    var listTokens = $('#listTokens');
                    $(N).each(function (index, x) {
                        var option = $("<option />");
                        option.html(x);
                        option.val(x);
                        listTokens.append(option);
                    });
                }
            });
        });
    });
</script>
<select id="listTokens" style="width: 100px">
</select>
<input type="button" id="btnPrintToken" value="Print Token" />

Controller

public class HomeController : Controller
{
    // GET: /Home/
    public ActionResult Index()
    {
        return View();
    }

    public JsonResult btnPrintToken()
    {
        if (Session["Tokenqueu"] == null)
        {
            Queue<int> queueToken = new Queue<int>();
            Session["Tokenqueu"] = queueToken;
        }
        List<int> listnumbertoken = new List<int>();
        Queue<int> tokenQueue = (Queue<int>)Session["Tokenqueu"];
        //string mesg = "There are" + tokenQueue.Count.ToString() + "Customer in the queue before you";
        if (Session["Lastnumbertoken"] == null)
        {
            Session["Lastnumbertoken"] = 0;
        }
        int nextnumbertoken = (int)Session["Lastnumbertoken"] + 1;
        Session["Lastnumbertoken"] = nextnumbertoken;
        tokenQueue.Enqueue(nextnumbertoken);

        foreach (int token in tokenQueue)
        {
            listnumbertoken.Add(token);
        }

        return Json(listnumbertoken, JsonRequestBehavior.AllowGet);
    }
}

Screenshot