Create a web form model

Asked

Viewed 25 times

0

I’m putting together a web page with . Net Core and I have a web form that has many fields and I was thinking about how to get all this information filled out more dynamically, I asked a question here and they gave me the suggestion to make a model to get this information, but I’m a beginner and I don’t know how to do it, I already did a search and started too, but I’m getting all the values null, I’ll leave +/- as is my code, please help me.

MODEL:

public class Persist
{
    public string[] data {get;set;} = {};
}

I left just one example, so it doesn’t get long, if I understand one, I try to do the others myself.

FORM:

<form method="POST">
    <select class="form-control w-100" id="data2" name="data[2]">            
        <option value="N"> Não analisar</option>            
        <option value="S"> Sim</option>        
    </select>

    <select class="form-control w-100" id="data5" name="data[5]">            
        <option value="N"> Não analisar</option>            
        <option value="S"> Sim</option>        
    </select>

    <select class="form-control w-100" id="data8" name="data[8]">            
        <option value="N"> Não analisar</option>            
        <option value="S"> Sim</option>        
    </select>
</form>

Function that picks up the data:

private static string Save(Models.Persist p)
{
    var teste = p.data[2]; // Dá erro

    return teste;
}

When I debug the code and try to see the value of p (the Model), all variables appear, but all variables null, how do I get the value of the fields?

I’m a beginner.

Grateful.

EDIT:

With the modification of model, the value of data now appears {string[0]}

  • @dill, Razor Pages is not?

  • @Leandroangelo

  • adds the rest of the example, because your code snippet is not complete

No answers

Browser other questions tagged

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