How to pass checkbox list to Actionresult

Asked

Viewed 898 times

3

I need to know how to pass the values of inputs checkboxes to the action Edit, need to pass the properties Checked and Id and store in a list of type Photo. Noting that the controller that this being executed is not of the same type as the view, this control is of gallery, the model gallery has a property of type List<Photo>.

Follows below the prints.

Model:

inserir a descrição da imagem aqui

View: inserir a descrição da imagem aqui

Controller: inserir a descrição da imagem aqui

Print from the screen to understand the context: inserir a descrição da imagem aqui

  • Where are the Checkboxes?

  • @Ciganomorrisonmendez updated the question with the print of the general context.

  • What you are trying to do subverts the MVC standard. The controller never sees the view but the other way around.

1 answer

4


Make your checkboxes look like this:

<input type="checkbox" name="photoToDeleteIds" value="@imagePath.IdPhoto" />

And change your controller’s action by adding a parameter photoToDeleteIds:

public ActionResult Edit(Gallery gallery, int[] photoToDeleteIds)
{
    // agora o parâmetro photoToDeleteIds contém os IDs das fotos que foram selecionadas
}

Or change the class Gallery, including a property PhotoToDeleteIds:

class Gallery
{
    public int[] PhotoToDeleteIds { get; set; }
    // ... restante da classe
}

Note that there is correspondence between the names of input and the parameter or property, depending on the path you choose. This match is not case sensitive/lower case.

  • 1

    perfect, worked! D

  • This part is working perfectly but I would like to know why the Gallery type parameter that the action receives, this only with ID and Title filled...

  • I already figured it out, because I would have to go through an Alper Hiddenfor to be able to pass as parameter :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.