Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

158
Visualizações
What to return after Ajax call asp.net

After ajax call is completed and the model is posted successfully to the controller, what should the controller return?

In my case, I just want to add an item to the wishlist and that's it, no redirect.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Controller can return a message or somethingelse for sure that your action did successful

over 4 years ago · Santiago Trujillo Relatório

0

This question you need to know two points, 1.What type can asp.net core return? 2.What type can ajax can receive.

First, Asp.net core can return the following types: Specific type, IActionResult, ActionResult<T>, Learn more details in this document.

Second, Ajax can send and receive information in various formats, including JSON, XML, HTML, and text files.

From your question, I think you want to recive the model from controller and add it to the wishlist in the view. So, In my opinion, You can directly return the specified model, Asp.net core will serialize models to Json Automatically. Then you can use it in your ajax success method.

simple demo:

<div class="text-center" id="Test">
    <h1 class="display-4">Welcome</h1>
    <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>

<button onclick="GetDetails(2)">submit</button>

@section Scripts{
    <script>
        function GetDetails(id){
             var postData = {
                    'ProductId': id,    
                };
            $.ajax({          
                type: "Post",
                url: "/Home/privacy",
                data: postData,              
                success: function (res) { 
                    document.getElementById("Test").innerHTML = res["name"];
                }
     });
                
    }
</script>
}

Controller

List<Student> students = new List<Student>()
            {
                new Student()
                {
                    Id="1",
                    Name="Jack",
                    Country="USA"
                },
                new Student()
                {
                    Id="2",
                    Name="Nacy",
                    Country="Uk"
                },
                new Student()
                {
                    Id="3",
                    Name="Lily",
                    Country="Cn"
                }
            };

[HttpPost]
        public Student Privacy(string ProductId)
        {
            var result = students.Where(x => x.Id == ProductId).FirstOrDefault();
            return result;
        }
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda