Ask Your Question

How can I use one POST request to update the statuses of multiple instances in Razor ASP.NET?

asked 2023-03-24 11:00:00 +0000

djk gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2021-06-04 02:00:00 +0000

ladyg gravatar image

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>
  1. In your controller, create an action method that receives the form data and updates the instances accordingly:
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;
    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.

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools


Asked: 2023-03-24 11:00:00 +0000

Seen: 8 times

Last updated: Jun 04 '21