Why does my Address template return null after the postback? [duplicate]

0

Why does my Endereco template return null after the postback?:

Template:

publicclassPaciente{publicPaciente(){Endereco=newList<Endereco>();}[Key]publicintPacienteID{get;set;}publicstringNome{get;set;}publicstringProfissao{get;set;}publicvirtualICollection<Endereco>Endereco{get;set;}}publicclassEndereco{[Key]publicintEnderecoID{get;set;}publicintPacienteID{get;set;}publicstringLogradouro{get;set;}publicstringNOLogradouro{get;set;}publicvirtualPacientePaciente{get;set;}}

HTML

@modelSGMed.Dominio.Entidade.Paciente@{ViewBag.Title=null;}@using(Html.BeginForm("Alterar", "Paciente", FormMethod.Post))
    {
    <p>
        <input type="submit" value="Salvar Paciente" class="btn btn-primary" />
    </p>
    <div class="row">
        <div class="col-md-12">
            <fieldset>
                <legend>Dados do Paciente</legend>            
                    <div class="col-md-7">
                        @Html.LabelFor(model => model.Nome)
                        @Html.TextBoxFor(model => model.Nome, new { @class = "form-control", style = "min-width:100%;" })
                    </div>                           
                    <div class="col-md-2">
                        <label>Profissão</label>
                        <input type="text" id="Profissao" name="Profissao" value="@Model.Profissao" class="form-control" style="min-width:100%;" />
                    </div>
            </fieldset>
        </div>
        <br />
        <div class="col-md-12">
            <fieldset>
                <legend>Dados do Endereço</legend>
                    @foreach(var item in Model.Endereco) 
                    {                   
                        <div class="row">
                            <div class="col-md-6">
                                <label>Logradouro</label>                                
                                <input type="text" id="Logradouro" name="Endereco.Logradouro" value="@item.Logradouro" class="form-control" style="min-width:100%;" />
                            </div>
                            <div class="col-md-1">
                                <label>Número</label>
                                <input type="text" id="NOLogradouro" name="Endereco.NOLogradouro" value="@item.NOLogradouro" class="form-control" style="min-width:100%;" />
                            </div>
                        </div>
                    }
            </fieldset>
        </div>
    </div>
    
asked by anonymous 29.04.2018 / 16:23

1 answer

0

Building View to edit lists and collections is somewhat quirky and has some pitfalls. This is due to the attributes name and id of the elements vs how MVC will bind with its ViewModel .

In your Model, change the type of ICollection<> to IList<> , so you can access its members by an index

public virtual IList<Endereco> Endereco { get; set; }

Now in your View, instead of using foreach for a for , constructing the elements as follows.

<div class="col-md-12">
    <fieldset>
        <legend>Dados do Endereço</legend>
        @for (int enderecoIndex = 0; enderecoIndex < Model.Endereco.Count; enderecoIndex++)
        {
            @Html.HiddenFor(x => @Model.Endereco[enderecoIndex].EnderecoID)
            @Html.HiddenFor(x => @Model.Endereco[enderecoIndex].PacienteID)
            <div class="row">
                <div class="col-md-6">
                    <label>Logradouro</label>
                    @Html.TextBoxFor(x => Model.Endereco[enderecoIndex].Logradouro, new { @class = "form-control", style = "min-width:100%;" })                            
                </div>
                <div class="col-md-1">
                    <label>Número</label>
                    @Html.TextBoxFor(x => Model.Endereco[enderecoIndex].NOLogradouro, new { @class = "form-control", style = "min-width:100%;" })                            
                </div>
            </div>

        }
    </fieldset>
</div>
    
30.04.2018 / 20:06