Hi kavithav,
Check this example. Now please take its reference and correct your code.
Model
public class StudentModel
{
public int Id { get; set; }
public string Name { get; set; }
public string Rollno { get; set; }
public bool id { get; set; }
}
Controller
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
{
List<StudentModel> model = new List<StudentModel>();
model.Add(new StudentModel { Id = 1, Name = "Ram", Rollno = "A001", id = true });
model.Add(new StudentModel { Id = 2, Name = "Hari", Rollno = "A002", id = false });
model.Add(new StudentModel { Id = 3, Name = "Rahim", Rollno = null, id = true });
model.Add(new StudentModel { Id = 4, Name = null, Rollno = "A004", id = true });
TempData["StudentList"] = model;
return View();
}
}
View
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Index</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$('[id*=BtnView]').on('click', function () {
alert("Id is : " + $(this).closest('.row').find('[id*=HdnId]').val());
});
});
</script>
</head>
<body>
<% List<StudentModel> list = (List<StudentModel>)TempData["StudentList"]; %>
<div id="divmain" style="width: 100%" class="Table brd">
<%foreach (var items in list)
{%>
<div class="row" style="width: 100%">
<div class="test">
<%if ((items.Name != null)) {%>
<p><%=items.Name %></p>
<%} else {%>
<p>N/A</p>
<%}%>
</div>
<div class="test">
<%if ((items.Rollno != null)) {%>
<p><%=items.Rollno %>
</p>
<%} else {%>
<p>N/A
</p>
<%}%>
</div>
<div class="test">
<%if ((items.id == true)) {%>
<input type="button" id="BtnView" class="View BtnClsView" value="View" style="width: 100px;" />
<% } else {%>
<p>N/A</p>
<%}%>
</div>
<div class="test">
<input type="hidden" value='<%=items.Id %>' id="HdnId" />
</div>
</div>
<hr />
<%} %>
</div>
</body>
</html>
Screenshot