πŸ“„ Documentation: _myModalPreview.cshtml


πŸ“š Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Key Functional Areas
  4. View Model & Data Model
  5. Markup Structure
  6. API Endpoint: RenderImagePrev
  7. Helper Methods & Utilities
  8. Error Handling & Edge Cases
  9. UML Diagram
  10. Usage Examples
  11. Sample Code Blocks
  12. 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

  1. 🎨 Modal Structure
    • Header, Body, Footer
  2. πŸ”— Action Link
    • Calls RenderImagePrev with parameters (id, preview)
  3. πŸ“‘ Embedded Document
    • Renders a PDF via <embed> tag
  4. πŸ–¨οΈ 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

  • FileContentResult with appropriate Content-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

  1. ❌ Invalid id
    • Controller should return 404 NotFound() if document not found.
  2. ⚠️ Unsupported MimeType
    • Graceful fallback (e.g., display β€œPreview not available”).
  3. πŸ“΅ 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 type attribute dynamically.

Enjoy seamless document previews in your application! πŸš€