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
– Ricardo Pontual
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
– edro
@Ricardopunctual left as would be the tag formed in the view in the question, take a look
– edro
but is with the
id
fixed, in addition to pass to the model should usename
. must do in your view afor
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">
– Ricardo Pontual
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
– edro
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.– edro