Hi Ainguahj,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of NorthWind database that you can download using the link given below.
Download Northwind Database
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
NorthwindEntities entities = new NorthwindEntities();
return View(from customer in entities.Customers.Take(5)
select customer);
}
}
View
@model IEnumerable<_Print_Selected_Row.Customer>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div id="Grid">
<table>
<tr>
<th>CustomerID</th>
<th>ContactName</th>
<th>City</th>
<th>Country</th>
<th>Action</th>
</tr>
@foreach (Customer customer in Model)
{
<tr>
<td>@customer.CustomerID</td>
<td>@customer.ContactName</td>
<td>@customer.City</td>
<td>@customer.Country</td>
<td><img src="~/img/how-to-print-3.png" id="PrintIcon" alt="Print" onclick="Print(this);" /></td>
</tr>
}
</table>
</div>
<script type="text/javascript">
function Print(element) {
var printWindow = window.open('', '', 'height=200,width=400');
printWindow.document.write('<html><head><title>Selected Row</title>');
printWindow.document.write('</head><body >');
printWindow.document.write(element.parentNode.parentNode.textContent);
printWindow.document.write('</body></html>');
printWindow.document.close();
printWindow.print();
}
</script>
</body>
</html>
Screenshot
