To use one POST request to update the statuses of multiple instances in Razor ASP.NET, you can follow these steps:
public class StatusUpdateModel {
public int[] InstanceIds { get; set; }
public string NewStatus { get; set; }
}
@model StatusUpdateModel
<form method="post" action="@Url.Action("UpdateStatus", "Instances")">
@foreach (var id in Model.InstanceIds) {
<input type="hidden" name="InstanceIds[]" value="@id" />
}
<label>New Status:</label>
<input type="text" name="NewStatus" value="@Model.NewStatus" />
<button type="submit">Update Status</button>
</form>
[HttpPost]
public IActionResult UpdateStatus(StatusUpdateModel model) {
foreach (var id in model.InstanceIds) {
var instance = db.Instances.FirstOrDefault(i => i.Id == id);
if (instance != null) {
instance.Status = model.NewStatus;
db.SaveChanges();
}
}
return RedirectToAction("Index");
}
Note: This is just one way to accomplish this task. You could also use AJAX to perform the update asynchronously, handle errors and validation, and use more advanced models to represent the data.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-03-24 11:00:00 +0000
Seen: 10 times
Last updated: Jun 04 '21
What are the steps to troubleshoot the reason for the failures in AWS ALB health checks?
How can two GCP instances with static IPs be created using Terraform?
What is the meaning of two databases on a Postgres server?
What is the method for utilizing jq with streams to parse information about Amazon's RDS instances?
How can ec2 instances be created in every availability zone using Terraform?
What is the process of creating instances of interfaces called?
How can FormSet changes be saved when related models are involved?