Hi @Anjali Agarwal,
If you can't change the database table then you can add the [NotMapped]
attribute to Type
property after changing the model.
I made a simple example, you can use it as a reference.
You want the query form to be on the same page as the displayed data, but since your form uses SelfServiceTransaction
and the displayed data uses List<Transaction>
, I created a ViewModel
to contain both Models.
Models:
public class Transaction
{
[Key]
public int TransactionId { get; set; }
public string? ReceiptNumber { get; set; }
public string? EmailAddress { get; set; }
public string? Items { get; set; }
//Add this attribute
[NotMapped]
public string? Type { get; set; }
public string? status { get; set; }
public virtual List<TransactionItem> Transactions { get; set; } = new List<TransactionItem>();
}
public partial class TransactionItem
{
public int TransactionId { get; set; }
[Key]
public int ItemId { get; set; }
public string? TransactionItemXml { get; set; }
}
public class SelfServiceTransaction
{
public string ReceiptNumber { get; set; }
public string EmailAddress { get; set; }
}
public class ViewModel
{
public SelfServiceTransaction SelfServiceTransactions { get; set; }
public List<Transaction> TransactionList { get; set; }
}
Controller:
[HttpGet]
public IActionResult Display()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Display(ViewModel trans)
{
List <Transaction> TransList = getTransactionById(trans.SelfServiceTransactions);
ViewModel viewmodel = new ViewModel();
viewmodel.TransactionList = TransList;
return View(viewmodel);
}
private List<Transaction> getTransactionById(SelfServiceTransaction trans)
{
var transaction = _context.Transaction.Where(t => t.ReceiptNumber == trans.ReceiptNumber && t.EmailAddress == trans.EmailAddress).ToList();
foreach (var item in transaction)
{
item.Type = "ReType";
}
return transaction;
}
Display.cshtml:
@model TestAPP.Models.ViewModel
<div style="margin-top:20px">
<form asp-action="Display" method="post">
<div asp-validation-summary=ModelOnly class="text-danger"></div>
<div class="form-group row">
<div class="col">
<label asp-for="@Model.SelfServiceTransactions.ReceiptNumber" class=" control-label required" style="font-weight:bold;"></label>
<input asp-for="@Model.SelfServiceTransactions.ReceiptNumber" class="form-control" />
<span asp-validation-for="@Model.SelfServiceTransactions.ReceiptNumber" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<div class="col">
<label asp-for="@Model.SelfServiceTransactions.EmailAddress" class=" control-label required" style="font-weight:bold;"></label>
<input asp-for="@Model.SelfServiceTransactions.EmailAddress" class="form-control" />
<span asp-validation-for="@Model.SelfServiceTransactions.EmailAddress" class="text-danger"></span>
</div>
</div>
<div>
<input type="submit" value="Search" />
</div>
</form>
</div>
<div>
@if (Model != null && Model.TransactionList.Count() != 0)
{
<table class="table">
<thead>
<tr>
<th>Status</th>
<th>Items</th>
<th>Type</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.TransactionList)
{
<tr>
<th>@item.status</th>
<th>@item.Items</th>
<th>@item.Type</th>
</tr>
}
</tbody>
</table>
}
</div>
Test Result:
When I input and click Search:
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best Regards,
Chen Li