Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To use one POST request to update the statuses of multiple instances in Razor ASP.NET, you can follow these steps:

  1. Create a model class that represents the data you want to update:
public class StatusUpdateModel {
    public int[] InstanceIds { get; set; }
    public string NewStatus { get; set; }
}
  1. In your Razor view, create a form that posts the data to the appropriate action method:
@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>
  1. In your controller, create an action method that receives the form data and updates the instances accordingly:
[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.