clicking on create button and having the create view appear on current view page

I have a project that has a request form with number render partials of view page. Within the render partials is a create record button which allows the user to create a record in a certain model. I’ve been trying to get the create view page to appear on the request form page when clicking on the create record button. I’m not sure if having 2 partialasyncs on the same view are my issue but it’s not appearing as I’d like. In the picture you’ll see that the create view appears when loading the request form, I want the create page to only appear after clicking the create record button. Any help would be great and my code is below.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@model RequestViewModel
<div style="background-color:lightgray; width:25%">
<table>
<tr>
<td>
<table class="table table-bordered table-striped" style="width:75%;
background-color:white; border:1px solid black;">
<thead>
<tr>
<th><!--Edit--></th>
<th><!--Delete--></th>
<th>#</th>
<th>Category</th>
<th>Item</th>
<th>Qty</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<input type="hidden" asp-for="Rqkey"/>
<input type="hidden" asp-for="Szkey"/>
@{
if(Model.Seizures != null)
{
int rownum = 0;
foreach (Seizure sez in Model.Seizures)
{
<tr id="@rownum">
<td><input type="button" value="Update" onclick="showSeizure(@sez.Szkey); hide(@rownum)" /></td>
<td><a asp-action="DeleteSeizure" onclick="return confirm('Are you sure you want to remove @sez.Item?')" asp-route-id="@sez.Szkey">Delete</a></td>
<td><!--#--></td>
<td>@sez.Category</td>
<td>@sez.Item</td>
<td>@sez.Qty</td>
<td>@sez.Value</td>
</tr>
<tr style="display:none" id="@sez.Szkey">
@await Html.PartialAsync("UpdateSeizure", sez)
</tr>
<tr style="display:none" id="@sez.Rqkey">
@await Html.PartialAsync("CreateSeizure", sez)
<input type="button" value="Create Record" onclick="showcreate(@sez.Rqkey)" />
</tr>
rownum++;
}
}
}
</tbody>
</table>
</td>
</tr>
</table>
</div>
<script type="text/javascript">
function showSeizure(key){
document.getElementById(key).style.display = "contents";
}
function hide(rownum){
document.getElementById(rownum).style.display = "none";
}
function showcreate(key){
document.getElementById(key).style.display = "contents";
}
function hidetable(rownum) {
document.getElementById(rownum).style.display = "none";
}
</code>
<code>@model RequestViewModel <div style="background-color:lightgray; width:25%"> <table> <tr> <td> <table class="table table-bordered table-striped" style="width:75%; background-color:white; border:1px solid black;"> <thead> <tr> <th><!--Edit--></th> <th><!--Delete--></th> <th>#</th> <th>Category</th> <th>Item</th> <th>Qty</th> <th>Value</th> </tr> </thead> <tbody> <input type="hidden" asp-for="Rqkey"/> <input type="hidden" asp-for="Szkey"/> @{ if(Model.Seizures != null) { int rownum = 0; foreach (Seizure sez in Model.Seizures) { <tr id="@rownum"> <td><input type="button" value="Update" onclick="showSeizure(@sez.Szkey); hide(@rownum)" /></td> <td><a asp-action="DeleteSeizure" onclick="return confirm('Are you sure you want to remove @sez.Item?')" asp-route-id="@sez.Szkey">Delete</a></td> <td><!--#--></td> <td>@sez.Category</td> <td>@sez.Item</td> <td>@sez.Qty</td> <td>@sez.Value</td> </tr> <tr style="display:none" id="@sez.Szkey"> @await Html.PartialAsync("UpdateSeizure", sez) </tr> <tr style="display:none" id="@sez.Rqkey"> @await Html.PartialAsync("CreateSeizure", sez) <input type="button" value="Create Record" onclick="showcreate(@sez.Rqkey)" /> </tr> rownum++; } } } </tbody> </table> </td> </tr> </table> </div> <script type="text/javascript"> function showSeizure(key){ document.getElementById(key).style.display = "contents"; } function hide(rownum){ document.getElementById(rownum).style.display = "none"; } function showcreate(key){ document.getElementById(key).style.display = "contents"; } function hidetable(rownum) { document.getElementById(rownum).style.display = "none"; } </code>
@model RequestViewModel
<div style="background-color:lightgray; width:25%">
<table>
    <tr>
        <td>
            <table class="table table-bordered table-striped" style="width:75%;
            background-color:white; border:1px solid black;">
            <thead>
                    <tr>
                        <th><!--Edit--></th>
                        <th><!--Delete--></th>
                        <th>#</th>
                        <th>Category</th>
                        <th>Item</th>
                        <th>Qty</th>
                        <th>Value</th>
                    </tr>
            </thead>
            <tbody>
                <input type="hidden" asp-for="Rqkey"/>
                <input type="hidden" asp-for="Szkey"/>
                   
                    @{
                        if(Model.Seizures != null)
                        {
                            int rownum = 0;
                            foreach (Seizure sez in Model.Seizures)
                            {
                                                <tr id="@rownum">
                                                    <td><input type="button" value="Update" onclick="showSeizure(@sez.Szkey); hide(@rownum)" /></td>
                                                    <td><a asp-action="DeleteSeizure" onclick="return confirm('Are you sure you want to remove @sez.Item?')" asp-route-id="@sez.Szkey">Delete</a></td>
                                                    <td><!--#--></td>
                                                    <td>@sez.Category</td>
                                                    <td>@sez.Item</td>
                                                    <td>@sez.Qty</td>
                                                    <td>@sez.Value</td>
                                                </tr>
                                                <tr style="display:none" id="@sez.Szkey">
                                                    @await Html.PartialAsync("UpdateSeizure", sez)
                                                </tr>
                                                <tr style="display:none" id="@sez.Rqkey">
                                                    
                                                        @await Html.PartialAsync("CreateSeizure", sez)
                                                        <input type="button" value="Create Record" onclick="showcreate(@sez.Rqkey)" />
                                                   

                                                </tr>
                                                rownum++;
                            }
                        }
                    }
            </tbody>
                
            </table>
        </td>
    </tr>
    
</table>

</div>
<script type="text/javascript">
function showSeizure(key){
    document.getElementById(key).style.display = "contents";
}
function hide(rownum){
    document.getElementById(rownum).style.display = "none";
}
function showcreate(key){
    document.getElementById(key).style.display = "contents";
}
function hidetable(rownum) {
    document.getElementById(rownum).style.display = "none";
}

Normally, we will use model popup for this to avoid show all the partial view inside the table.

We could use ajax to fetch the partival view’s codes and use js to add it to the model popup div, then when the user click button, it will show the model popup to let the user type in the username and password then go to the save page.

More details, you could refer to below example:

View:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@model RequestViewModel
<div style="background-color:lightgray; width:50%; margin: auto;">
<h2>Request #@Model.Rqkey</h2>
<table class="table table-bordered table-striped" style="background-color:white; border:1px solid black;">
<thead>
<tr>
<th>Edit</th>
<th>Delete</th>
<th>#</th>
<th>Category</th>
<th>Item</th>
<th>Qty</th>
<th>Value</th>
<th>Create Record</th>
</tr>
</thead>
<tbody>
@if (Model.Seizures != null)
{
int rownum = 0;
foreach (var seizure in Model.Seizures)
{
<tr id="row-@rownum">
<td><input type="button" value="Update" onclick="showSeizure(@seizure.Szkey); hide(@rownum)" /></td>
<td><a asp-action="DeleteSeizure" onclick="return confirm('Are you sure you want to remove @seizure.Item?')" asp-route-id="@seizure.Szkey">Delete</a></td>
<td>@rownum + 1</td>
<td>@seizure.Category</td>
<td>@seizure.Item</td>
<td>@seizure.Qty</td>
<td>@seizure.Value.ToString("C")</td>
<td>
<!-- Button to trigger modal -->
<button type="button" class="btn btn-primary" onclick="showCreate(@seizure.Rqkey)">
Create Record
</button>
</td>
</tr>
<tr style="display:none" id="@seizure.Szkey">
@* @await Html.PartialAsync("UpdateSeizure", seizure)
*@ </tr>
rownum++;
}
}
</tbody>
</table>
</div>
<!-- Modal Structure -->
<div class="modal fade" id="createModal" tabindex="-1" role="dialog" aria-labelledby="createModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="createModalLabel">Create Seizure</h5>
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
</div>
<div class="modal-body" id="modal-body-content">
</div>
</div>
</div>
</div>
@section Scripts{
<script type="text/javascript">
function showSeizure(key) {
document.getElementById(key).style.display = "contents";
}
function hide(rownum) {
document.getElementById(rownum).style.display = "none";
}
function showCreate(rqKey) {
// Fetch the CreateSeizure partial content using AJAX
fetch('@Url.Action("CreateSeizurePartial")?rqKey=' + rqKey)
.then(response => response.text())
.then(data => {
// Insert the fetched partial view into the modal body
document.getElementById('modal-body-content').innerHTML = data;
// Show the modal
$('#createModal').modal('show');
});
}
</script>
}
</code>
<code>@model RequestViewModel <div style="background-color:lightgray; width:50%; margin: auto;"> <h2>Request #@Model.Rqkey</h2> <table class="table table-bordered table-striped" style="background-color:white; border:1px solid black;"> <thead> <tr> <th>Edit</th> <th>Delete</th> <th>#</th> <th>Category</th> <th>Item</th> <th>Qty</th> <th>Value</th> <th>Create Record</th> </tr> </thead> <tbody> @if (Model.Seizures != null) { int rownum = 0; foreach (var seizure in Model.Seizures) { <tr id="row-@rownum"> <td><input type="button" value="Update" onclick="showSeizure(@seizure.Szkey); hide(@rownum)" /></td> <td><a asp-action="DeleteSeizure" onclick="return confirm('Are you sure you want to remove @seizure.Item?')" asp-route-id="@seizure.Szkey">Delete</a></td> <td>@rownum + 1</td> <td>@seizure.Category</td> <td>@seizure.Item</td> <td>@seizure.Qty</td> <td>@seizure.Value.ToString("C")</td> <td> <!-- Button to trigger modal --> <button type="button" class="btn btn-primary" onclick="showCreate(@seizure.Rqkey)"> Create Record </button> </td> </tr> <tr style="display:none" id="@seizure.Szkey"> @* @await Html.PartialAsync("UpdateSeizure", seizure) *@ </tr> rownum++; } } </tbody> </table> </div> <!-- Modal Structure --> <div class="modal fade" id="createModal" tabindex="-1" role="dialog" aria-labelledby="createModalLabel" aria-hidden="true"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="createModalLabel">Create Seizure</h5> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button> </div> <div class="modal-body" id="modal-body-content"> </div> </div> </div> </div> @section Scripts{ <script type="text/javascript"> function showSeizure(key) { document.getElementById(key).style.display = "contents"; } function hide(rownum) { document.getElementById(rownum).style.display = "none"; } function showCreate(rqKey) { // Fetch the CreateSeizure partial content using AJAX fetch('@Url.Action("CreateSeizurePartial")?rqKey=' + rqKey) .then(response => response.text()) .then(data => { // Insert the fetched partial view into the modal body document.getElementById('modal-body-content').innerHTML = data; // Show the modal $('#createModal').modal('show'); }); } </script> } </code>
@model RequestViewModel


<div style="background-color:lightgray; width:50%; margin: auto;">
    <h2>Request #@Model.Rqkey</h2>
    <table class="table table-bordered table-striped" style="background-color:white; border:1px solid black;">
        <thead>
            <tr>
                <th>Edit</th>
                <th>Delete</th>
                <th>#</th>
                <th>Category</th>
                <th>Item</th>
                <th>Qty</th>
                <th>Value</th>
                <th>Create Record</th>
            </tr>
        </thead>
        <tbody>
            @if (Model.Seizures != null)
            {
                int rownum = 0;
                foreach (var seizure in Model.Seizures)
                {
                    <tr id="row-@rownum">
                        <td><input type="button" value="Update" onclick="showSeizure(@seizure.Szkey); hide(@rownum)" /></td>
                        <td><a asp-action="DeleteSeizure" onclick="return confirm('Are you sure you want to remove @seizure.Item?')" asp-route-id="@seizure.Szkey">Delete</a></td>
                        <td>@rownum + 1</td>
                        <td>@seizure.Category</td>
                        <td>@seizure.Item</td>
                        <td>@seizure.Qty</td>
                        <td>@seizure.Value.ToString("C")</td>
                        <td>
                            <!-- Button to trigger modal -->
                            <button type="button" class="btn btn-primary" onclick="showCreate(@seizure.Rqkey)">
                                Create Record
                            </button>
                        </td>
                    </tr>
                    <tr style="display:none" id="@seizure.Szkey">
@*                         @await Html.PartialAsync("UpdateSeizure", seizure)
 *@                    </tr>
                    rownum++;
                }
            }
        </tbody>
    </table>
</div>

<!-- Modal Structure -->
<div class="modal fade" id="createModal" tabindex="-1" role="dialog" aria-labelledby="createModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="createModalLabel">Create Seizure</h5>
                 <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>

            </div>
            <div class="modal-body" id="modal-body-content">
             </div>
          
        </div>
    </div>
</div>
@section Scripts{


     
    <script type="text/javascript">
        function showSeizure(key) {
            document.getElementById(key).style.display = "contents";
        }

        function hide(rownum) {
            document.getElementById(rownum).style.display = "none";
        }

        function showCreate(rqKey) {
            // Fetch the CreateSeizure partial content using AJAX
            fetch('@Url.Action("CreateSeizurePartial")?rqKey=' + rqKey)
                .then(response => response.text())
                .then(data => {
                    // Insert the fetched partial view into the modal body
                    document.getElementById('modal-body-content').innerHTML = data;
                    // Show the modal
                    $('#createModal').modal('show');
                });
        }
    </script>
}

Controller:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> private static List<Seizure> mockSeizures = new List<Seizure>
{
new Seizure { Szkey = 1, Rqkey = 100, Category = "Electronics", Item = "Laptop", Qty = 5, Value = 2500.00m },
new Seizure { Szkey = 2, Rqkey = 100, Category = "Furniture", Item = "Chair", Qty = 10, Value = 1500.00m }
};
public IActionResult UpdateRecord() {
var model = new RequestViewModel
{
Rqkey = 100,
Seizures = mockSeizures
};
return View(model);
}
public IActionResult CreateSeizurePartial(int rqKey)
{
var seizure = new Seizure { Rqkey = rqKey };
return PartialView("CreateSeizure", seizure);
}
</code>
<code> private static List<Seizure> mockSeizures = new List<Seizure> { new Seizure { Szkey = 1, Rqkey = 100, Category = "Electronics", Item = "Laptop", Qty = 5, Value = 2500.00m }, new Seizure { Szkey = 2, Rqkey = 100, Category = "Furniture", Item = "Chair", Qty = 10, Value = 1500.00m } }; public IActionResult UpdateRecord() { var model = new RequestViewModel { Rqkey = 100, Seizures = mockSeizures }; return View(model); } public IActionResult CreateSeizurePartial(int rqKey) { var seizure = new Seizure { Rqkey = rqKey }; return PartialView("CreateSeizure", seizure); } </code>
    private static List<Seizure> mockSeizures = new List<Seizure>
{
    new Seizure { Szkey = 1, Rqkey = 100, Category = "Electronics", Item = "Laptop", Qty = 5, Value = 2500.00m },
    new Seizure { Szkey = 2, Rqkey = 100, Category = "Furniture", Item = "Chair", Qty = 10, Value = 1500.00m }
};
    public IActionResult UpdateRecord() {

        var model = new RequestViewModel
        {
            Rqkey = 100,
            Seizures = mockSeizures
        };

        return View(model);
    }

    public IActionResult CreateSeizurePartial(int rqKey)
    {
        var seizure = new Seizure { Rqkey = rqKey };
        return PartialView("CreateSeizure", seizure);
    }

Partial view:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@model Seizure
<form asp-action="CreateSeizure" method="post">
<div>
<label>Category:</label>
<input asp-for="Category" class="form-control" />
</div>
<div>
<label>Item:</label>
<input asp-for="Item" class="form-control" />
</div>
<div>
<label>Quantity:</label>
<input asp-for="Qty" type="number" class="form-control" />
</div>
<div>
<label>Value:</label>
<input asp-for="Value" type="number" step="0.01" class="form-control" />
</div>
<input type="hidden" asp-for="Rqkey" />
<button type="submit">Save</button>
</form>
</code>
<code>@model Seizure <form asp-action="CreateSeizure" method="post"> <div> <label>Category:</label> <input asp-for="Category" class="form-control" /> </div> <div> <label>Item:</label> <input asp-for="Item" class="form-control" /> </div> <div> <label>Quantity:</label> <input asp-for="Qty" type="number" class="form-control" /> </div> <div> <label>Value:</label> <input asp-for="Value" type="number" step="0.01" class="form-control" /> </div> <input type="hidden" asp-for="Rqkey" /> <button type="submit">Save</button> </form> </code>
@model Seizure

    <form asp-action="CreateSeizure" method="post">
        <div>
            <label>Category:</label>
            <input asp-for="Category" class="form-control" />
        </div>
        <div>
            <label>Item:</label>
            <input asp-for="Item" class="form-control" />
        </div>
        <div>
            <label>Quantity:</label>
            <input asp-for="Qty" type="number" class="form-control" />
        </div>
        <div>
            <label>Value:</label>
            <input asp-for="Value" type="number" step="0.01" class="form-control" />
        </div>
        <input type="hidden" asp-for="Rqkey" />
        <button type="submit">Save</button>
    </form>

Result:

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật