Learning to use AJAX in ASP.Net MVC 4

Asked

Viewed 1,437 times

1

In my project, I have two models different, generating with this, their controllers and your views. Only what happens is that I don’t know how to use AJAX and urgently needed a help from you. What happens is, I wanted to render the views of a controller in a view other’s 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 view student details the occurrences he has and list them, add a new one and edit an existing one. That is, record information of occurrences without having to leave the view of student details. I know that using AJAX I have how to do this, only I can’t find cool articles or videos about. Every time I think, I get more confused. Could someone create a basic example so I can follow and learn how to use AJAX?

1 answer

3


You can even 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 load function in the controller you want (in this case, student details)

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

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

Example of Action to carry the view Details

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

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

EDIT

What I’m trying to explain to you downstairs is this::

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

JAVASCRIPT:

Viewcomum.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)
}

/Occurrences/Details

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

Partialview Alunos:

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

Partialview Occurrences:

@model List<BDOleoTorres.Models.Ocorrencias>
//Aqui constrois a tua partial com os dados dos ocorrencuas que queres
  • I understood and then I would put in these partitals the data that interests me right ? But when pulling them, I could pull a partial of occurrences in the partial of students without any problem ? Or I’d have to create a third party to pull the necessary information ?

  • Yes, in these partitals you can use the data that matters. Do you want to use Occurrences with Students in the same table? All mixed? Without yes, I advise you to create a common Viewmodel, where you mix the data of both

  • Viewmodel you speak is a view that is not typed right ? Where I mix the data I want to get.

  • No, Viewmodel is like an entity (or table, whatever you want to call it), where you build the fields you want. Make a Viewmodel with the Students and Occurrences data and it is solved. Note that a Viewmodel does not belong to the database

  • How would I make this Viewmodel ? Some example ? And how do I make it not mapped in the bank ? Then after I made this viewmodel the views would be automatically generated along with the controller ?

  • I don’t think you understand anything I’m explaining here anymore. I have already presented a possible solution (not realizing if it is well what you want or not). Do another post, is better

  • No no. I’m sorry, I ask too much. I’ll try to do it here. I’m already here, so I’ll implement it. If I have any error or anything like that, I will post here again ! Thank you very much.

Show 2 more comments

Browser other questions tagged

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