Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

How to access ViewData from external javascript ?

$
0
0

Hi

i've use this external javascript function to access ViewData variable(s) :

function deleteOrderItem(orderID) {
    if (confirm('Are you sure want to delete this item?')) {$.ajax({
            type: "POST",
            url: "DeleteOrder",
            data: { id: orderID },
            success: function () {
                setTimeout(function () {
                    debugger;
                    var personID = "@ViewData['_personID']";
                    viewItem(personID);
                }, 500)
            }
        });
    }
}

But at runTime, when i'm using developer tools, the personID contains @ViewData[_personID] as text and not accessing real viewData which i've set in my action method!

Here is my action method :

// POST: Orders/Delete/5
        [HttpPost]
        public async Task<IActionResult> DeleteOrder(int id)
        {
            var orders = await _dbContext.Orders.FindAsync(id);

            ViewData["_personID"] = orders.PersonId;

            _dbContext.Orders.Remove(orders);
            await _dbContext.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

Where is the problem & how to solve it?

Thanks in advance


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>