“Le contrôleur MVC 5 renvoie la valeur JSON à afficher” Réponses codées

Le contrôleur MVC 5 renvoie la valeur JSON à afficher

// mvc controller
 [HttpPost]
        public ActionResult IndChecking(string dta, string isChk)
        {
          /////
           return this.Json (new { Data = result, cKey = ContainerKey }, JsonRequestBehavior.AllowGet);
        }

// view
    success: function (result) {
          alert(result.cKey);
          
    }
BlackSwan

Comment retourner les données JSON de MVC Controller pour afficher

When you do return Json(...) you are specifically telling MVC not to use a view, and to serve serialized JSON data. Your browser opens a download dialog because it doesn't know what to do with this data.

If you instead want to return a view, just do return View(...) like you normally would:

var dictionary = listLocation.ToDictionary(x => x.label, x => x.value);
return View(new { Values = listLocation });
Then in your view, simply encode your data as JSON and assign it to a JavaScript variable:

<script>
    var values = @Html.Raw(Json.Encode(Model.Values));
</script>
Santino

Réponses similaires à “Le contrôleur MVC 5 renvoie la valeur JSON à afficher”

Questions similaires à “Le contrôleur MVC 5 renvoie la valeur JSON à afficher”

Plus de réponses similaires à “Le contrôleur MVC 5 renvoie la valeur JSON à afficher” dans JavaScript

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code