sridharnetha
sridharnetha

Reputation: 2238

how to assign a value to mvc tempdata using jquery

I know how to get MVC TempData value using JQuery, but here I am trying to assign some value to TempData using JQuery. Is this possible to assign value to MVC TempData by using JQuery?

I am using MVC4 razor, by clicking on some text it should redirect to ActionResult Index () page then query to the DB where assigned TempData value then response to the same page. I don’t want to display my value in the address bar.

View contains the following code

<div onclick="getorder(1)">Test</div>

<script type="text/javascript">
$(document).ready(function () {
    getorder = function (id) {
        alert("Test Message");
        '@TempData["OrdID"]' = id;
        //here redirect to ActionResult
    }
});

Controller ActionReslut Index()

 public ActionResult Index()
 {
     ViewModel.CheckOut model = new ViewModel.CheckOut();
     if (TempData["ordID"] != null)
     {                
        int OrderID = int.Parse(TempData["OrdID"].ToString());                
        if (OrderID != 0)
        {
            model.OrderedLineItems = db.OrderedLineItemRepository.GetAllByRefID(f => f.OrderIDFlag == OrderID).ToList();
        }
     }
     return View(model);
 }

Upvotes: 0

Views: 9045

Answers (1)

Tariq Albajjali
Tariq Albajjali

Reputation: 337

You can try Query Strings (GET Param), or you can put the javascript in the same HTML view (Not recommended).

Upvotes: 0

Related Questions