{"id":1983,"date":"2021-06-16T12:00:00","date_gmt":"2021-06-16T10:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/build-a-complete-app-with-asp-net-c-sql-lite-part-7\/"},"modified":"2026-07-05T03:25:54","modified_gmt":"2026-07-05T01:25:54","slug":"build-a-complete-app-with-asp-net-c-sql-lite-part-7","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/build-a-complete-app-with-asp-net-c-sql-lite-part-7\/","title":{"rendered":"Build a Complete App with ASP.Net, C#, SQL Lite \u2013 Part 7"},"content":{"rendered":"<p>This is part 7 of our complete application with ASP.Net, C# and SQL Lite. In this part, we would be working with images.<\/p>\n<ul>\n<li>First we would add some images to the home page.<\/li>\n<li>Then we add some image thumbnails to the tables<\/li>\n<\/ul>\n<p>We would cover the following in this tutorial:<\/p>\n<ol>\n<li><a href=\"#t1\">Adding the Home Images<\/a><\/li>\n<li><a href=\"#t2\">Adding Patient Thumbnail Images<\/a><\/li>\n<li><a href=\"#t3\">Add a Modal Popup<\/a><\/li>\n<li><a href=\"#t4\">Pass Clicked Image to the Modal<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t1\">1. Adding the Home Images<\/strong><\/h5>\n<p><strong>Step 1<\/strong> &#8211; First move the two home page images (HomePix.jpg and AppointmentPix.jpg). Move them into the <em>wwwroot\/assets\/img directory<\/em>.<\/p>\n<p><strong>Step 2<\/strong> &#8211; In the index page page, replicate the row that contains the charts.<\/p>\n<p><strong>Step 3<\/strong> &#8211; Remove the canvas sections. So we&#8217;ll have the card-body section left. Here, we&#8217;ll place the images<\/p>\n<p><strong>Step 5<\/strong> &#8211; You can now place the images using the markup shown below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;div<\/span> <span style=\"color: #0000cc;\">class=<\/span><span style=\"background-color: #fff0f0;\">\"card-body\"<\/span><span style=\"color: #007700;\">&gt;<\/span>\r\n    <span style=\"color: #007700;\">&lt;img<\/span> <span style=\"color: #0000cc;\">width=<\/span><span style=\"background-color: #fff0f0;\">\"100%\"<\/span> <span style=\"color: #0000cc;\">src=<\/span><span style=\"background-color: #fff0f0;\">\"~\/assets\/img\/HomePix.jpg\"<\/span> <span style=\"color: #007700;\">\/&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;\/div&gt;<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t2\">2. Adding the Patients Thumbnails<\/strong><\/h5>\n<p>Now we would add the patient&#8217;s image thumbnails to the Patients tables. So when this thumbnails is clicked, a popup of an enlarged images is displayed.<\/p>\n<p><strong>Step 1<\/strong> &#8211; Create a folder called patients inside the wwwroot\/assets\/img directory. Place your pictures there. Rename the images to 1.jpg, 2.jpg&#8230; etc. Also add a generic image ( I call it user.jpg) in the \/img directory.<\/p>\n<p><strong>Step 2<\/strong> &#8211; Open the Patients index view.<\/p>\n<p><strong>Step 3<\/strong> &#8211; First adjust the code to display the Country Name instead of the CountryId.<\/p>\n<p><strong>Step 4<\/strong> &#8211; Add one more column header to the before the first column of the table<\/p>\n<p><strong>Step 5<\/strong> &#8211; Add one more column to the body section like so:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;td&gt;<\/span>\r\n    <span style=\"color: #007700;\">&lt;img<\/span> <span style=\"color: #0000cc;\">width=<\/span><span style=\"background-color: #fff0f0;\">\"40\"<\/span> <span style=\"color: #0000cc;\">height=<\/span><span style=\"background-color: #fff0f0;\">\"40\"<\/span> <span style=\"color: #0000cc;\">src=<\/span><span style=\"background-color: #fff0f0;\">\"\/assets\/img\/user.jpg\"<\/span><span style=\"color: #007700;\">\/&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;\/td&gt;<\/span>\r\n<\/pre>\n<p>You can now launch the application and test the code<\/p>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t3\">3. Add a Popup Modal<\/strong><\/h5>\n<p>We want to be able to click on the image and have it displayed in a modal popup.<\/p>\n<p><strong>Step 1<\/strong> &#8211; Go to the Bootstrap website and copy a modal template<\/p>\n<p><strong>Step 2<\/strong> &#8211; Add one more column to your table and a button to open the modal. You may have to add the CDN links to your _Layout.cshtml file<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;link<\/span> <span style=\"color: #0000cc;\">rel=<\/span><span style=\"background-color: #fff0f0;\">\"stylesheet\"<\/span> <span style=\"color: #0000cc;\">href=<\/span><span style=\"background-color: #fff0f0;\">\"https:\/\/maxcdn.bootstrapcdn.com\/bootstrap\/3.4.1\/css\/bootstrap.min.css\"<\/span><span style=\"color: #007700;\">&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;script <\/span><span style=\"color: #0000cc;\">src=<\/span><span style=\"background-color: #fff0f0;\">\"https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/3.4.1\/jquery.min.js\"<\/span><span style=\"color: #007700;\">&gt;&lt;\/script&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;script <\/span><span style=\"color: #0000cc;\">src=<\/span><span style=\"background-color: #fff0f0;\">\"https:\/\/maxcdn.bootstrapcdn.com\/bootstrap\/3.4.1\/js\/bootstrap.min.js\"<\/span><span style=\"color: #007700;\">&gt;&lt;\/script&gt;<\/span>\r\n<\/pre>\n<p>Test the modal and make sure it works. <span style=\"font-size: 1.125rem;\">Once you&#8217;re sure the modal works, you can delete the column.<\/span><\/p>\n<p><strong>Step 3<\/strong> &#8211; Now, surround the &lt;img&gt; with an anchor tag &lt;a&gt; and set the href attribute to same as the src attribute of the &lt;img&gt; <em>(\/assets\/img\/user.jpg)<\/em>. The anchor tag has an id attribute of &#8220;photoButton&#8221;.<\/p>\n<p><strong>Step 4<\/strong> &#8211; In the body of the modal form, add an &lt;img&gt; tag with id of &#8220;patientPhoto&#8221; and width and height attributes of 95%. Also add <em>src<\/em> attribute of empty string &#8220;&#8221;.\u00a0 See below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;div<\/span> <span style=\"color: #0000cc;\">class=<\/span><span style=\"background-color: #fff0f0;\">\"modal-body\"<\/span><span style=\"color: #007700;\">&gt;<\/span>\r\n    <span style=\"color: #007700;\">&lt;img<\/span> <span style=\"color: #0000cc;\">id=<\/span><span style=\"background-color: #fff0f0;\">\"patientPhoto\"<\/span> <span style=\"color: #0000cc;\">src=<\/span><span style=\"background-color: #fff0f0;\">\"\"<\/span> <span style=\"color: #0000cc;\">width=<\/span><span style=\"background-color: #fff0f0;\">\"95%\"<\/span> <span style=\"color: #0000cc;\">height=<\/span><span style=\"background-color: #fff0f0;\">\"95%\"<\/span> <span style=\"color: #007700;\">\/&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;\/div&gt;<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 5<\/strong> &#8211; <a href=\"https:\/\/code.jquery.com\/jquery-3.6.0.min.js\" target=\"_blank\" rel=\"noopener\">Download JQuery from here<\/a> and add it to<em> js<\/em> folder in the <em>wwwroot<\/em>.<\/p>\n<p><strong>Step 6<\/strong> &#8211; In your index.cshtml patient list file add the following line:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;script <\/span><span style=\"color: #0000cc;\">src=<\/span><span style=\"background-color: #fff0f0;\">\"~\/js\/jquery-3.6.0.min.js\"<\/span><span style=\"color: #007700;\">&gt;&lt;\/script&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;script&gt;<\/span>\r\n    $(<span style=\"background-color: #fff0f0;\">'.table #photoButton'<\/span>).on(<span style=\"background-color: #fff0f0;\">'click'<\/span>, <span style=\"color: #008800; font-weight: bold;\">function<\/span> (event) {\r\n        event.preventDefault();\r\n        <span style=\"color: #008800; font-weight: bold;\">var<\/span> href <span style=\"color: #333333;\">=<\/span> $(<span style=\"color: #008800; font-weight: bold;\">this<\/span>).attr(<span style=\"background-color: #fff0f0;\">'href'<\/span>);\r\n        $(<span style=\"background-color: #fff0f0;\">'#photoModal #patientPhoto'<\/span>).attr(<span style=\"background-color: #fff0f0;\">'src'<\/span>, href);\r\n        $(<span style=\"background-color: #fff0f0;\">'#photoModal'<\/span>).modal(<span style=\"background-color: #fff0f0;\">'show'<\/span>);\r\n    });\r\n<span style=\"color: #007700;\">&lt;\/script&gt;<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>At this point, launch the application and test it to make sure it works.<\/p>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t4\">4. Pass Selected Photo to Modal<\/strong><\/h5>\n<p>First, we have to display the photos based on the particular records that is showing. There are a number of ways to do this, but we&#8217;ll do it the easy way.<\/p>\n<p>First, we would name the images same as the PatientId. So we have images: 1.jpg, 2.jpg, 3.jpg and so on.<\/p>\n<p>Then for each record, we would append the directory path to the filename like so:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;tbody&gt;<\/span>\r\n    @foreach (var item in Model)\r\n    {\r\n        var image = \"\/assets\/img\/patients\/\" + item.PatientId + \".jpg\";\r\n\r\n        <span style=\"color: #007700;\">&lt;tr&gt;<\/span>\r\n            <span style=\"color: #007700;\">&lt;td&gt;<\/span>\r\n                <span style=\"color: #007700;\">&lt;a<\/span> <span style=\"color: #0000cc;\">id=<\/span><span style=\"background-color: #fff0f0;\">\"photoButton\"<\/span> <span style=\"color: #0000cc;\">href=<\/span><span style=\"background-color: #fff0f0;\">\"@image\"<\/span><span style=\"color: #007700;\">&gt;<\/span>\r\n                    <span style=\"color: #007700;\">&lt;img<\/span> <span style=\"color: #0000cc;\">width=<\/span><span style=\"background-color: #fff0f0;\">\"40\"<\/span> <span style=\"color: #0000cc;\">height=<\/span><span style=\"background-color: #fff0f0;\">\"40\"<\/span> <span style=\"color: #0000cc;\">src=<\/span><span style=\"background-color: #fff0f0;\">\"@image\"<\/span> <span style=\"color: #007700;\">\/&gt;<\/span>\r\n                <span style=\"color: #007700;\">&lt;\/a&gt;<\/span>\r\n            <span style=\"color: #007700;\">&lt;\/td&gt;<\/span>\r\n...\r\n...\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>So\u00a0 when you click on the link, the javascript code executes. This script takes the href attribute of the anchor tag (which is the same as the src attribute of the img tag) and uses it to set the src attribute of the patientPhoto in the modal.<\/p>\n<p>So now, if you get everything right, launch the application and test it out!<\/p>\n<p>If you have challenges, please watch the video, leave me a comment or connect with me.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is part 7 of our complete application with ASP.Net, C# and SQL Lite. In this part, we would be working with images. First we &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[311],"tags":[],"class_list":["post-1983","post","type-post","status-publish","format-standard","hentry","category-sql"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1983","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=1983"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1983\/revisions"}],"predecessor-version":[{"id":2151,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1983\/revisions\/2151"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1983"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}