You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core 3.1:Razor向Fancybox传数据并展示表格方案问询

Hey there! Let's work through how to get your Fancybox popup showing those applicant details correctly with your ASP.NET Core 3.1 Razor and DataTable setup. I'll break this down into actionable steps, plus share some resources to help you learn more.

Solution: Display Applicant Details in Fancybox Popup

Your core goals are: hide itemName and requestStatus from the main DataTable, trigger a Fancybox popup on "Details" click, and pass the right data to show those fields in the popup. Here's how to make it happen:

Step 1: Clean Up Your Razor Table & Prepare Data for the Popup

First, remove the itemName and requestStatus columns from your main table (since you don't want them visible there). Then modify the "Details" link to store the necessary data directly using HTML5 data-* attributes—this lets us pass data to the popup without extra server calls (great if your data is already loaded in the view).

Updated table code:

<thead>
<tr id="headerrow">
<th> Name </th>
<th> Requested item count </th>
<th> delivered request </th>
<th> pending request count </th>
<th> registered request count </th>
<th> operations </th>
<!-- Removed itemName/requestStatus headers -->
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td> @Html.DisplayFor(modelItem => item.applicantName) </td>
<td> @Html.DisplayFor(modelItem => item.requestedApiCount) </td>
<td> @Html.DisplayFor(modelItem => item.deliveredApiCount) </td>
<td> @Html.DisplayFor(modelItem => item.pendingApiCount) </td>
<td> @Html.DisplayFor(modelItem => item.inProcessApiCount) </td>
<td>
<!-- Modified Details link with data attributes -->
<a href="javascript:void(0)" class="fancybox-details" 
   data-applicant-id="@item.applicantID"
   data-item-name="@Html.DisplayFor(modelItem => item.itemName)"
   data-request-status="@Html.DisplayFor(modelItem => item.requestStatus)">Details</a> |
<a asp-action="Edit" asp-route-id="@item.applicantID">Edit</a> |
<a asp-action="Delete" asp-route-id="@item.applicantID">Delete</a>
</td>
<!-- Removed itemName/requestStatus columns -->
</tr>
}
</tbody>

Step 2: Configure Fancybox to Show Dynamic Table Content

Next, update your JavaScript to listen for the "Details" click, pull the stored data, and generate a table inside the Fancybox popup.

Updated script:

jQuery(document).ready(function ($) {
    // Initialize DataTable as before
    $("#myDummyTable").DataTable({
        "language": { "url": "//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/Persian.json" }
    });

    // Handle Fancybox popup for details
    $(".fancybox-details").on("click", function() {
        // Grab data from the link's attributes
        const applicantId = $(this).data("applicant-id");
        const itemName = $(this).data("item-name");
        const requestStatus = $(this).data("request-status");

        // Build the popup content with a table
        const popupHtml = `
            <div class="popup-details">
                <h4>Applicant #${applicantId} Details</h4>
                <table class="table table-bordered">
                    <thead>
                        <tr>
                            <th>Item Name</th>
                            <th>Request Status</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>${itemName}</td>
                            <td>${requestStatus}</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        `;

        // Open Fancybox with the custom content
        $.fancybox.open({
            content: popupHtml,
            type: 'html',
            width: 550,
            title: `Applicant Details: #${applicantId}`
        });
    });
});

Optional: Fetch Data via AJAX (For Real-Time/Server-Side Data)

If you need to pull fresh data from the server instead of using pre-rendered data, swap the click handler with this AJAX version:

$(".fancybox-details").on("click", function() {
    const applicantId = $(this).data("applicant-id");

    // Fetch details from your controller
    $.ajax({
        url: `@Url.Action("GetApplicantDetails", "YourControllerName")`,
        type: "GET",
        data: { id: applicantId },
        success: function(data) {
            const popupHtml = `
                <div class="popup-details">
                    <h4>Applicant #${applicantId} Details</h4>
                    <table class="table table-bordered">
                        <thead>
                            <tr>
                                <th>Item Name</th>
                                <th>Request Status</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td>${data.itemName}</td>
                                <td>${data.requestStatus}</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            `;

            $.fancybox.open({
                content: popupHtml,
                type: 'html',
                width: 550,
                title: `Applicant Details: #${applicantId}`
            });
        },
        error: function() {
            alert("Oops, failed to load details.");
        }
    });
});

Then add this action to your controller:

public IActionResult GetApplicantDetails(int id)
{
    // Replace with your data fetch logic (e.g., from database)
    var details = _yourRepository.GetApplicantDetailsById(id);
    return Json(new { itemName = details.itemName, requestStatus = details.requestStatus });
}

Step 3: Style the Popup (Optional)

Add some CSS to make the popup table look clean:

.popup-details h4 {
    margin-bottom: 1rem;
    text-align: center;
}
.popup-details .table {
    margin: 0 auto;
    width: 90%;
}

Learning Resources

Since you mentioned struggling to find non-image Fancybox examples:

  • Focus on Fancybox's HTML Content documentation section—it covers all the basics of loading dynamic HTML (like tables) into popups.
  • Search for "Fancybox dynamic HTML content" for community-driven examples of generating popup content on the fly.
  • For DataTable integration, check DataTable's Event Handling docs if you ever want to trigger popups via row clicks instead of links.

内容的提问来源于stack exchange,提问作者MinaMRM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 19:17:38