Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
Qué devolver después de la llamada Ajax asp.net

Después de que se complete la llamada ajax y el modelo se publique correctamente en el controlador, ¿qué debe devolver el controlador?

En mi caso, solo quiero agregar un artículo a la lista de deseos y listo, sin redirección.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

El controlador puede devolver un mensaje o algo más seguro de que su acción fue exitosa

over 4 years ago · Santiago Trujillo Report

0

Esta pregunta necesita saber dos puntos, 1. ¿Qué tipo puede devolver el núcleo asp.net? 2. ¿Qué tipo puede recibir ajax?

Primero , el núcleo de Asp.net puede devolver los siguientes tipos: Specific type , IActionResult , ActionResult<T> , obtenga más información en este documento .

En segundo lugar , Ajax puede enviar y recibir información en varios formatos, incluidos JSON , XML , HTML, y text files .

Según su pregunta, creo que desea recibir el modelo del controlador y agregarlo a la lista de deseos en la vista. Entonces, en mi opinión, puede devolver directamente el modelo especificado, el núcleo de Asp.net serializará los modelos a Json automáticamente. Entonces puedes usarlo en tu método de éxito ajax.

demostración sencilla:

 <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> }

Controlador

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!