6
I’ve been trying to apply the solution to this question (How to pass checkbox list to Actionresult) in a problem I’m having but I’m not getting a positive result.
I’m getting an array with several on’s instead of receiving an array with the id’s, as I imagined it would be.
I have the following classes:
Controle
:
public class Controle
{
public Controle() {
Actions = new List<Action>();
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
public string DisplayName { get; set; }
public virtual List<Acao> Acoes { get; set; }
}
Acao
:
public class Acao
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public int ControleId { get; set; }
public string Name { get; set; }
[ForeignKey("ControleId")]
public Controle Controle { get; set; }
}
Grupo
, which would represent the groups in which users are allocated:
public class Grupo
{
public Grupo() {
Acessos = new List<GrupoAcesso>();
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Descricao { get; set; }
[InverseProperty("Grupo")]
public virtual List<GrupoAcesso> Acessos { get; set; }
}
And GrupoAcesso
, representing the Controles
and Acoes
that the Group has access to:
public class GrupoAcesso
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public int GrupoId { get; set; }
public int ControleId { get; set; }
public int AcaoId { get; set; }
[ForeignKey("GrupoId")]
public virtual Grupo Grupo { get; set; }
[ForeignKey("ControleId")]
public virtual Controle Controle { get; set; }
[ForeignKey("AcaoId")]
public virtual Acao Acao { get; set; }
}
Until then I’m trying to create a view that presents me something like:
To list the Acoes
I’m doing it this way:
@model Domain.Grupo
....
@{
var checkeds = new string[] { };
if (ViewBag.Checkeds != null) {
checkeds = ViewBag.Checkeds as string[];
}
}
...
<table class="table table-striped table-condensed table-bordered" style="margin:2px;">
@for (var X = 0; X < controle.Acoes.Count(); X++)
{
var acao= controle.Acoes[X];
<tr>
<td>
<label style="font-weight:bold; color:darkblue">@acao.Name</label>
</td>
<td class="text-center" style="width:50px; padding:0; vertical-align:middle;">
<input type="checkbox" name="checkeds" id="@acao.Id"
if (checkeds.Contains(acao.Id.ToString())) { <text> checked </text> } />
</td>
</tr>
}
</table>
...
But in my method I get the post, the result of this "game" made with the checkboxes is returning a list of on’s instead of a list of Id’s.
My method, which is not yet complete, I am testing yet:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Gravar([Bind(Exclude = "Acessos, Usuarios")]Domain.Grupo obj, string[] checkeds)
{
if (ModelState.IsValid)
{
try
{
// carrego o grupo caso ele já exista
var rec = repository.Grupos.Include("Acessos").SingleOrDefault(x => x.Id == obj.Id || x.Descricao.ToLower() == obj.Descricao.ToLower());
if (rec != null) {
rec.Acessos.RemoveAll(x => x.ControleId == rec.Id);
}
else {
repository.Grupos.Add(obj);
}
repository.SaveChanges();
return RedirectToAction("Listar");
}
catch (Exception e)
{
ModelState.AddModelError("", "Ocorreu um erro enquanto processávamos sua requisição");
System.Console.WriteLine(e.Message);
}
}
ViewBag.Controllers = repository.Controles.Include("Acoes").ToList();
ViewBag.Checkeds = checkeds;
return View("Cadastro", obj);
}
The parameter checkeds
:
The parameter checkeds come with the list of on’s.
So what I ask is for your help to elaborate this View and make it exchange information correctly with my Action method.
Hello @Andrefigueiredo, thanks for the help. I indexed, only that I am receiving in the array all indexes with the value "0", instead of the Id of Action. You know what it could be?
– user3628
If I keep leaving the parameter as string[] in my
Action
, the array then continues coming with the list of on’s, move to int[] returns the array with all 0. So apparently it’s just returned a "positive" (true) to the selected indexes.– user3628
Ah, you have to return then a complex type (a Viewmodel to) or a dictionary Key : Action Id, Value: status checked.
– Andre Figueiredo
Eita.. Can you give me an example, dear? Please!
– user3628
I updated the answer, you would have to modify the items. But overall it would be something in this scheme..
– Andre Figueiredo
ASP.NET MVC cuts when the sequence is not complete.. like this: If you look at the sample screen image you will understand. I selected List, Add, jumped the Alter and added the Rule out and the Report to the Neighborhood. But my
Dictionary
comes only with the List and the Add. But thank you, I think I understand that and I’ll run some tests. Thank you!– user3628
yes. if the inputs have a sequence [0, 1, 3], example, the framework will cut between 1 and 3. You have to be careful when giving the POST elements to be in sequence (in the example, I tried using the X counter)
– Andre Figueiredo
Cool, I can try to manipulate this with Javascript/jQuery. I’ll try!
– user3628