1 | initial version |
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.