Learning to use AJAX in ASP.Net MVC 4

1

In my project, I have two different models , thus generating their controllers and their views. The only thing that happens is that I do not know how to use AJAX and I needed an urgent help from you. What happens is that I wanted to render the views of a controller into a view of another controller . Explaining better for example, I have a model, controller and Student views and a model, controller and occurrence views. What happens, I wanted to render in the student's view details the occurrences he has and list them, add a new one and edit an existing one. That is, to record event information without having to leave the view of student details. I know that using AJAX I have how to do this, but I can not find legal articles or videos. Every time I think about it, I get more confused. Could anyone create a basic example so I can follow and learn how to use AJAX?

    
asked by anonymous 13.05.2014 / 19:31

1 answer

3

So you can go through Jquery and it's simpler:

$("#divOcorrencia").empty().load('/Aluno/Details');

So, you create a div where the details of the students will appear, and you make load of the function in the controller you want (in this case, student details)

If you want to load a view without going to controller you can do:

@Html.Partial("/PastaViewsAlunos/Detials");

Example of Action to load view Details

public ActionResult Details()
{
    var dadosAlunos = db.Alunos.ToList();
    return PartialView(dadosAlunos)
}

This function loads view Detials with student data (you must edit query to put the data you want to display)

EDIT

What I'm trying to explain below is the following:

You have a main view where you will take the partials with the information you want (in this case, students and occurrences)

BY JAVASCRIPT:

ViewCommunity.cshtml

<div id="divAlunos">
</div>

<div id="divOcorrencias">
</div>

<script>
    $("#divAlunos").empty().load('/Aluno/Details');
    $("#divOcorrencias").empty().load('/Ocorrencias/Details');
</script>

Controller: / Student / Details

public ActionResult Details()
{
    var dadosAlunos = db.Alunos.ToList();
    return PartialView(dadosAlunos)
}

/ Ocorrencias / Details

public ActionResult Details()
{
    var dadosOc = db.Ocorrencias.ToList();
    return PartialView(dadosOc)
}

PartialView Students:

@model List<BDOleoTorres.Models.Alunos>
//Aqui constrois a tua partial com os dados dos alunos que queres

PartialView Occurs:

@model List<BDOleoTorres.Models.Ocorrencias>
//Aqui constrois a tua partial com os dados dos ocorrencuas que queres
    
13.05.2014 / 19:38