π Documentation: _myModalPreview.cshtml
π Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Key Functional Areas
- View Model & Data Model
- Markup Structure
- API Endpoint:
RenderImagePrev - Helper Methods & Utilities
- Error Handling & Edge Cases
- UML Diagram
- Usage Examples
- Sample Code Blocks
- Conclusion
1. Overview & Purpose
File:
IrbyImaging/Views/Imaging/_myModalPreview.cshtml
This Razor partial view renders a Bootstrap modal to preview a document (PDF/image) directly within your web application. It leverages an <embed> tag to display the document fetched from the ImagingController.RenderImagePrev action.
Primary Objectives
- Display document details in a modal window
- Embed a PDF or image preview at full size with zoom support
- Provide Print and Close actions
2. Dependencies & Technologies
| Technology | Version / Notes |
|---|---|
| Razor Views | ASP.NET MVC / ASP.NET Core MVC |
| Bootstrap | β₯ 5.x (for modal styling and JavaScript events) |
| jQuery (opt.) | Optional, for advanced event handling |
| URL Helpers | @Url.Action |
Emoji Legend
π¦ β Bootstrap components
π β Routing / URL Helpers
π β PDF / Document embedding
3. Key Functional Areas
- π¨ Modal Structure
- Header, Body, Footer
- π Action Link
- Calls
RenderImagePrevwith parameters (id,preview)
- Calls
- π Embedded Document
- Renders a PDF via
<embed>tag
- Renders a PDF via
- π¨οΈ Print & Close Buttons
- Two buttons in the footer for user actions
4. View Model & Data Model
DocumentImaging (ViewModel)
namespace IrbyImaging.Models
{
public class DocumentImaging
{
public int TempDocumentId { get; set; }
// ... other properties (e.g., FileName, MimeType, etc.)
}
}
- TempDocumentId: Unique identifier for the temporary document to be previewed.
- Add additional metadata (e.g., Title, CreatedDate) as needed.
5. Markup Structure
@model IrbyImaging.Models.DocumentImaging
<div class="modal-content" style="height: 100%;">
<!-- 1. Header -->
<div class="modal-header">
<h5 class="modal-title" id="transaction-detailModalLabel">Document Details</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<!-- 2. Body (Embed PDF) -->
<div style="height: 600px">
<div class="modal-body" style="height: 100%;">
<embed
height="100%"
width="100%"
src="@Url.Action(
"RenderImagePrev",
"Imaging",
new { id = Model.TempDocumentId, preview = true }
)#zoom=FitH"
type="application/pdf">
</div>
</div>
<!-- 3. Footer -->
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Print Document</button>
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
</div>
</div>
Breakdown
| Section | Description |
|---|---|
.modal-header |
Title and close button (Γ) |
.modal-body |
Full-height container embedding the PDF with #zoom=FitH (fit horizontally) |
.modal-footer |
Action buttons: Print Document (currently closes modal) & Close |
6. API Endpoint: RenderImagePrev
// Controller: ImagingController.cs
public IActionResult RenderImagePrev(int id, bool preview = false)
{
// 1. Fetch document bytes by 'id'
// 2. Determine MIME type (PDF, JPEG, PNG, etc.)
// 3. Return File(documentBytes, mimeType);
}
| Parameter | Type | Description |
|---|---|---|
id |
int |
Document identifier (TempDocumentId) |
preview |
bool |
Flag indicating preview mode (true for zoom) |
Return
FileContentResultwith appropriateContent-Type(e.g.,application/pdf).
7. Helper Methods & Utilities
None in this view. All business logic resides in the
ImagingController.
Potential Extensions
- JavaScript helper to programmatically trigger Print via
window.print(). - Utility to adjust zoom level dynamically.
8. Error Handling & Edge Cases
- β Invalid
id- Controller should return
404 NotFound()if document not found.
- Controller should return
- β οΈ Unsupported MimeType
- Graceful fallback (e.g., display βPreview not availableβ).
- π΅ Browser PDF Plugin
- Ensure cross-browser compatibility or use PDF.js as a fallback.
9. UML Diagram
classDiagram
class DocumentImaging {
+int TempDocumentId
+string FileName
+string MimeType
}
class ImagingController {
+IActionResult RenderImagePrev(int id, bool preview)
}
DocumentImaging --> ImagingController : uses
10. Usage Examples
1οΈβ£ Render Partial from a Parent View
@model IrbyImaging.Models.YourParentViewModel
<button type="button"
class="btn btn-primary"
data-bs-toggle="modal"
data-bs-target="#myModalPreview">
Preview Document
</button>
@Html.Partial("_myModalPreview", new DocumentImaging {
TempDocumentId = Model.SelectedDocId
})
2οΈβ£ Triggering Modal via JavaScript
// Suppose you want to load by AJAX, then show:
$.get('/Imaging/GetDocumentImagingModel?id=123', function(model) {
$('#modalContainer').html(model.html);
var modal = new bootstrap.Modal($('#myModalPreview'));
modal.show();
});
11. Sample Code Blocks
<!-- Calling the embed directly: -->
<embed src="/Imaging/RenderImagePrev/123?preview=true#zoom=FitH"
width="100%" height="600px"
type="application/pdf" />
// Controller stub for RenderImagePrev:
[HttpGet]
public IActionResult RenderImagePrev(int id, bool preview = true)
{
var doc = _documentService.GetById(id);
if (doc == null) return NotFound();
return File(doc.Bytes, doc.MimeType);
}
12. Conclusion
The _myModalPreview.cshtml partial encapsulates a clean, reusable, and responsive modal for document preview. By separating the view from the controller logic, it:
- Keeps your MVC layers decoupled
- Leverages Bootstrapβs modal for a consistent UI/UX
- Utilizes
<embed>for in-browser PDF rendering
π§ Next Steps:
- Add client-side Print logic to trigger printing the embedded document.
- Enhance error feedback for unsupported file types.
- Extend for image previews (JPG, PNG) by setting the
typeattribute dynamically.
Enjoy seamless document previews in your application! π