Two models and a controller

Asked

Viewed 344 times

1

Following the reasoning of that other question of mine, where I create two models for manipulations, as if I could use a controller to make the manipulations in the tables ?

My intention in doing this is that in the same view i can register both information at once. Because I didn’t want to create a controller to make the modifications in the phone table and yes in the controller student.

Is that feasible? If not, I could use AJAX and if possible, examples?

  • Related: http://answall.com/questions/39055/partial-n%C3%A3o-%C3%A9-rendered/39056#39056

  • You want me to put that one in there too @Gypsy ?

  • 1

    No need. The link is already there on the right.

1 answer

1


...I could use a controller to do the manipulations on the tables ?

Actually the controller does not manipulate tables.

Commenting roughly, the controller receives requests from the GET/POST user, maps to Action and returns a View. You have no responsibility to manipulate tables, That would be your Model’s responsibility.

So you can for example:

1- Create a class called Alunoviewmodel, which will be a kind of container of all the information required to register the student (for example: name, telephone, address, etc.). This class may even have a method to save the student.

public class AlunoViewModel
{    
    [Required]
    public string Nome { get; set; }

    [DisplayName("Endereço")]
    public string Endereco { get; set; }

    [DisplayName("Telefone")]
    public string NumeroTelefone { get; set; }

    public ICollection<Telefone> Telefones {get;set;}

    // Demais propriedades

    // Método para salvar um aluno
    public static void Salvar(AlunoViewModel alunoViewModel)
    {
        //Seu código para salvar uma aluno
    }
}

2- Create a controller and insert Actions (one to display the registration screen "GET", another to receive the data informed "POST")

public class AlunoController: Controller
{
    public ActionResult Criar()
    {                    
       return View(new AlunoViewModel());
    }

    [HttpPost]
    public ActionResult Criar(AlunoViewModel alunoViewModel)
    {           
        //Agora usando as informações de alunoViewModel você chama o seu Model para criar os objetos (aluno, telefone, etc) e salvar no banco.
        AlunoViewModel.Salvar(alunoViewModel);
    }
}

3-Create Alunoviewmodel-like View to display in the register.

@model Models.AlunoViewModel    
@{
    ViewBag.Title = "Cadastro de Aluno";        
}

 @*

   Aqui vai o código em razor para exibir as informações do cadastro

*@

Yes, it is feasible to register the student’s phone information, parents, etc whether or not using AJAX.

In the case of student phone registration, you may have a property Numerophone (textbox type control for example) and a button for the user to add the phone to the list (Listbox type control for example). As soon as the user enters the number, click on the button, you add the phone entered in the Listbox (related to Phones property). In the post of the registration screen you will receive this list of phones and associates to the student to save.

  • in the controller there and in the viewmodel, 'Save' and 'Create' methods, I could use the code created by scaffolding in actions ? You could put an example code of how to do this insertion using list ?

Browser other questions tagged

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