Hi mahesh213,
To set the Session value you need to make ajax call to ActionResult and pass the id and text as parameter and set the session inside the ActionResult and redirect to the specified page to do the rest of functionality.
Home Controller
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
{
return View();
}
public ActionResult GetMenu()
{
List<Menu> lists = new List<Menu>();
lists.Add(new Menu { Id = 1, Text = "Employee", Value = "/Report/Index/" });
lists.Add(new Menu { Id = 2, Text = "Country", Value = "/Report/Index/" });
return Json(lists, JsonRequestBehavior.AllowGet);
}
[HttpPost]
public void SetSessionValue(string id, string text)
{
// Set Session values.
Session["Id"] = id;
Session["Text"] = text;
}
public class Menu
{
public int Id { get; set; }
public string Text { get; set; }
public string Value { get; set; }
}
}
Home View
<body>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$.ajax({
type: "POST",
url: "/Home/GetMenu",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
var ul = $('#unordered');
$.each(r, function (i, item) {
ul.append("<li><a href='" + item.Value + "' id='" + item.Id + "'>" + item.Text + "</a></li>");
});
}
});
$(document).on("click", "#unordered a", function (e) {
var id = $(this).attr('id');
var text = $(this).text();
var link = $(this).attr('href');
$.ajax({
type: "POST",
url: "/Home/SetSessionValue",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: "{id: '" + id + "',text: '" + text + "'}",
success: function (r) {
window.location.href = link;
},
error: function (r) {
}
});
});
});
</script>
<h4>An Unordered List:</h4>
<ul id="unordered"></ul>
</body>
Then in the report controller get the Session Values and do rest of task.