πŸ“„ _viewdoc.cshtml Documentation


πŸ“‘ Table of Contents

  1. Overview & Purpose
  2. File Location & Structure
  3. Dependencies & Technologies
  4. Model Binding
  5. View Markup Analysis
  6. API Endpoint Overview
  7. Usage Examples
  8. Customization & Configuration
  9. Error Handling & Fallbacks
  10. UML Diagram
  11. Conclusion

πŸ“ 1. Overview & Purpose

_viewdoc.cshtml is a partial Razor view designed to embed and preview a PDF document within a Bootstrap-styled layout. It:

  • Receives a DocumentImaging model instance.
  • Invokes the IViewController.DocumentPreview(int id) endpoint to fetch a PDF stream.
  • Renders the PDF inline at a specified size, with an initial FitH zoom level.

Use Case: Quickly preview invoice scans, medical images, or any PDF‐based document in a dashboard or details page without leaving the ASP.NET MVC application.


πŸ“‚ 2. File Location & Structure

IrbyImaging/
β”œβ”€β”€ Views/
β”‚   └── IView/
β”‚       └── _viewdoc.cshtml   ← This file
  • Naming: Leading underscore (_) denotes a partial view.
  • Parent Folder: Views/IView/ correlates with the IViewController.

πŸ”§ 3. Dependencies & Technologies

Layer Technology / Package
View Engine ASP.NET Core MVC / Razor
CSS Framework Bootstrap 4 / 5 (for col-lg-4)
PDF Rendering HTML <embed> element (native browser)
Model Namespace IrbyImaging.Models.DocumentImaging

Browser Support: Modern browsers (Chrome, Firefox, Edge, Safari) support <embed type="application/pdf">.


🧩 4. Model Binding

@model IrbyImaging.Models.DocumentImaging
  • DocumentImaging:
    • Likely contains properties such as DocumentImageId, FileName, CreatedDate, etc.
    • Required: DocumentImageId (int) to fetch the correct PDF.

πŸ–₯️ 5. View Markup Analysis

<div class="col-lg-4">
  <embed
    id="frPDF"
    height="800px"
    width="600px"
    src="@Url.Action("DocumentPreview", "IView", new { id = Model.DocumentImageId })#zoom=FitH"
    data-form-method="post"
    type="application/pdf">
</div>
Attribute Description
class="col-lg-4" Bootstrap grid class: spans 4 of 12 columns on large screens.
id="frPDF" Unique identifierβ€”useful for JavaScript hooks or CSS overrides.
height="800px" Vertical size of the embedded PDF.
width="600px" Horizontal size of the embedded PDF.
src="...#zoom=FitH" Calls DocumentPreview action, appends #zoom=FitH to fit horizontally.
data-form-method (Optional/Custom) Data attribute for any JavaScript form-handling logic you may implement.
type="application/pdf" MIME type hint that the resource is a PDF file.

How It Works

  1. Request: Browser requests the partial view as part of a parent page.
  2. Rendering: Razor engine injects the src URL with the document’s ID.
  3. PDF Fetch: The <embed> tag loads that URL, which returns a PDF stream.
  4. Display: PDF is rendered inside the browser with the specified dimensions.

πŸš€ 6. API Endpoint Overview

IViewController.DocumentPreview(int id)

  • Route: /IView/DocumentPreview/{id}
  • HTTP Method: GET (default)
  • Returns: FileStreamResult or FileContentResult with application/pdf content type.
  • Parameters:
    • id (int): The DocumentImageId from the model.
public IActionResult DocumentPreview(int id)
{
    var doc = _documentService.GetById(id);
    if (doc == null) return NotFound();
    return File(doc.Bytes, "application/pdf");
}

πŸ”§ 7. Usage Examples

7.1. Rendering in a Parent View

@model IrbyImaging.Models.DocumentImaging

<h2>Document Preview</h2>
@await Html.PartialAsync("_viewdoc", Model)

7.2. AJAX Load (optional)

// Load PDF preview on-demand into a div#previewContainer
$("#previewBtn").click(function() {
  $("#previewContainer").load(
    "/IView/_viewdoc",
    { DocumentImageId: selectedId }
  );
});

βš™οΈ 8. Customization & Configuration

  • Dimensions: Adjust height/width attributes to suit your layout.
  • Zoom: Change #zoom=FitH to #zoom=100 or other PDF.js parameters.
  • Styling:
    #frPDF {
      border: 1px solid #ddd;
      box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    }
    
  • Access Control: Ensure DocumentPreview checks user permissions before streaming.

πŸ› οΈ 9. Error Handling & Fallbacks

Scenario Behavior Solution
Document not found Controller returns 404 β†’ <embed> shows blank Provide a β€œNot Found” message
PDF load error Browser shows default PDF error page Wrap in <object> with fallback text or image.
Cross-origin issues (CORS) PDF fails to load if origin blocked Configure CORS in Startup.cs
<object data="..." type="application/pdf" width="600" height="800">
  <p>Unable to display PDF. <a href="..." target="_blank">Download instead.</a></p>
</object>

πŸ—ΊοΈ 10. UML Diagram

graph LR
  A[User Interface] --> B[Parent View] 
  B --> C[_viewdoc.cshtml Partial]
  C --> D[IViewController.DocumentPreview(id)]
  D --> E[DocumentImaging Service]
  E --> F[Database / Blob Store]
  D -->|PDF Stream| C
  C -->|Embed PDF| A

βœ… 11. Conclusion

The _viewdoc.cshtml partial is a focused, reusable component for in‐page PDF previews in your ASP.NET MVC app. By binding to a DocumentImaging model, calling a single controller action, and leveraging native browser PDF support, it offers a lightweight, performant solution.

πŸŽ‰ Next Steps:

  • Integrate authentication/authorization in your controller.
  • Enhance user experience with PDF.js for advanced controls (search, pagination).
  • Expand responsive behavior for mobile devices (use col-md-* classes or CSS media queries).

Happy coding! πŸš€