C# MVC5 - Table with Checkbox and Different Actions

Asked

Viewed 844 times

3

I am working on a project of which I own a page that lists some records, of which I will 'flirt' some to perform batch actions such as deletion, status change, among others.

How do I send this list of mine with the 'fledged' records to different actions?

My list is built like this:

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<table>
    <tr>
        <th>Selecione</th>
        <th>Descrição</th>
    </tr>
    @for (int r = 0; r < Model.Count(); r++)
    {
    <tr>
        <td>@Html.CheckBoxFor(i => i[r].Selecionado)</td>
        <td>@Html.DisplayFor(i => i[r].Descricao)</td>
    </tr>
    }
</table>

<input type="button" value="Alterar Selecionados Para Status x" />
<input type="button" value="Alterar Selecionados Para Status y" />
<input type="button" value="Alterar Selecionados Para Status z" />
}

In my post method, I receive the parameter as follows:

[...]
public ActionResult SelectLines(List<Objeto> list) { [...] }
  • Through Ajax. You’ve managed to generate this list so I can direct it as code?

  • Yes, I have an 'Index' action that has a list of objects with a Boolean field called 'selected'. If I do a single Ubmit, I can pass this list to my action by the POST method without any problems. But in cases of more than one action, I don’t know how to do it... How would I send the list via ajax for a specific action? via: date: $('table'). serialize() ?

  • Then, clicking the field would trigger Ajax. You can put in your question an example in code?

  • The big challenge would be for Ajax to know where to send the information. How is a Action different per line, there would have to be a way to jot down the information that led to the Action somewhere in the HTML.

  • If you want to act the same action in each thing that is sealed, it is much simpler. So you can click on as many checkboxes as you want, and click Submit only once. I would use the same Model you used to generate the list in your View. From there just turn the selected items into a JSON array

  • Putz... I confess that I was a bit confused... I edited my question, to try to further detail my difficulty. I can usually send the list to an action present in a form. In this action I see the list in search of the 'selected' items to start treating them.

Show 1 more comment

1 answer

2


The most elegant solution I know is to implement a button detection attribute of Submit of form:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class MultipleButtonAttribute : ActionNameSelectorAttribute
{
    public string Name { get; set; }
    public string Argument { get; set; }

    public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)
    {
        var isValidName = false;
        var keyValue = string.Format("{0}:{1}", Name, Argument);
        var value = controllerContext.Controller.ValueProvider.GetValue(keyValue);

        if (value != null)
        {
            controllerContext.Controller.ControllerContext.RouteData.Values[Name] = Argument;
            isValidName = true;
        }

        return isValidName;
    }
}

Change your buttons to:

<input type="submit" value="Alterar Selecionados Para Status x" name="status:x" />
<input type="submit" value="Alterar Selecionados Para Status y" name="status:y"/>
<input type="submit" value="Alterar Selecionados Para Status z" name="status:z"/>

The Controller would look like this:

[HttpPost]
[MultipleButton(Name = "status", Argument = "x")]
public ActionResult MudarParaStatusX(MeuViewModel mvm) { ... }

[HttpPost]
[MultipleButton(Name = "status", Argument = "y")]
public ActionResult MudarParaStatusY(MeuViewModel mvm) { ... }

[HttpPost]
[MultipleButton(Name = "status", Argument = "z")]
public ActionResult MudarParaStatusZ(MeuViewModel mvm) { ... }
  • Show!!! Very good! I only had one question: where should I implement this Multiplebuttonattribute class?

  • Create a directory called Attributes in the solution and implement there. The namespace should stay MeuProjeto.Attributes.

  • Got it! Solved the problem! Even, I managed to solve with the use of Ajax, passing the attribute 'date' as follows: $('form'). serialize() ; Also solved, but I do not know if it is a good practice, because it was very simple to implement... But I really liked your solution! Thank you!

  • @Antôniofilho The one with serialize is the Ajax approach I had commented on, but I’m prone to using traditional server requests for the sake of style. Feel free to use either of the two solutions.

  • I implemented this solution today, but it does not redirect correctly to the action. The expression: if (value != null) returns null always... And I’m doing exactly as described...

  • I studied this concept of Attributes and found some simpler examples, considering the attr name of the tag as the same action name. But in the override method of the Attributes class, it does not locate my Submit form...

  • As I told you, there was one detail wrong with the answer, which was the type of input, that is not button, and yes submit. I updated the answer.

  • I’m already using type="Ubmit"

Show 4 more comments

Browser other questions tagged

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