Hi mukesh1,
You can not set TempData thorugh javascript. You need to call server side function for that. So call server side method and set TempData.
Check this example. Now please take its reference and correct your code.
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public string Index(string tempDataValue)
{
TempData["Message"] = tempDataValue;
return TempData["Message"].ToString();
}
}
View
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type='text/javascript'>
function ckkreport() {
var s = document.getElementById("chklist").value;
$.ajax({
type: "POST",
url: '@Url.Action("Index", "Home")',
data: '{ tempDataValue:"' + s + '"}',
contentType: "application/json; charset=utf-8",
dataType: "text",
success: function (r) {
alert(r);
},
error: function (r) {
alert(r.responseText);
}
});
}
</script>
<input type="text" id="chklist" name="message" />
<input type='button' value='Update TempData' onclick='ckkreport();' />
</div>
</body>
</html>
Screenshot