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.
@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:
@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:
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:
@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