'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);
}
Solution 1:[1]
You can try Query Strings (GET Param), or you can put the javascript in the same HTML view (Not recommended).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Tariq Albajjali |