Taking values from a form using a model

Asked

Viewed 87 times

0

I’m creating a web form and I’ve been told here at Sopt to use a model, since I’m using . Net Core Razor. I am trying to create the model, to pass parameter but I cannot recover the values of the fields, since I am trying to use a List of strings. I’ll leave +/- as I’m doing down there so you can give me a hand in this, I’m new in technology. Thank you.

OBS: My fields are being generated automatically, so id’s, name’s, are very similar, like checkbox0, checkbox1, checkbox2...

MODEL:

public class cl{
    public List<string> checkbox { get; set; } = new List<string>();
}

Onpost of the page:

public IActionResult OnPost(cl c)
{
    var teste = "";
    for(int i = 0; i < 3; i++){
        c.checkbox.Add(""); // faço isso apenas para adicionar uma nova linha na lista
        teste += c.checkbox[i] + "; "; // os valores de c.checkbox[i] retornam "". eu gostaria que retornasse por exemplo 1 ou 0, para eu saber se está marcado ou não.
    }
}

RECALLING: The names/id's of the example’s checkbox: checkbox0, checkbox1, checkbox2.

EDIT1: HTML input checkbox:

<input type="checkbox" id="check0" name="checkbox0">

EDIT2: I’m doing this to automate, because if I were to do it manually, it would be:

public IActionResult OnPost(string checkbox0, string checkbox1, string checkbox2)
{
    var teste += checkbox0 + "; " + checkbox1 + "; " + checkbox2 + ";"; 
}
  • Can you input the code of how you’re generating the view? if the view is correctly generated just do a foreach on the Property and the values will be there correctly

  • the view is being generated correctly, with the Names and id’s as I said. Could you leave how this foreach would look in a reply ? You can use the same name’s I used in the @Ricardopunctual question example

  • @Ricardopunctual left as would be the tag formed in the view in the question, take a look

  • but is with the id fixed, in addition to pass to the model should use name. must do in your view a for to bind the items you have in the model, something like this: @for (var i = 0; i < model.checkbox.Count; i++) and then something like that: <input type="checkbox" asp-for="model[i].checkbox">

  • 1- I can’t have the fixed id, in this case ? 2- I don’t understand the utility of the second code... What is this Asp-for? Create checkbox according to for? @Ricardopunctul

  • I think in my code c.checkbox.Add(" "); I should put instead of " ", the value that is returning me from the checkbox, I just don’t know how to access this value.

Show 1 more comment

1 answer

0

If your model expects a collection of checkbox your form should provide this to her. And how it will find these inputs inFormData is through the attribute name.

It is unclear, in your question, how they are being generated automatically, but the correct thing is that they have id unique and a name that reflects your model. In this case checkbox[0], checkbox[1]... or simply checkbox[].

See the example below, considering the model presented:

Model:

public class cl{
    public List<string> checkbox { get; set; } = new List<string>();
}

Formulario.cshtml

<form method="post">
    <input type="checkbox" id="check1" name="checkbox[]" value="A"> A <br/>
    <input type="checkbox" id="check2" name="checkbox[]" value="B"> B <br/>
    <input type="checkbox" id="check3" name="checkbox[]" value="C"> C <br/>    
    <button type="submit">Enviar</button>
</form>

Formulario.cshtml.Cs

public void OnPost(cl checklist)
{
    var total = checklist.checkbox.Count;
}

Browser other questions tagged

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