Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

MVC 5 controller return json value to view

// 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);
          
    }
Comment

how to return json data from mvc controller to view

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

PREVIOUS NEXT
Code Example
Javascript :: javscript assert 
Javascript :: remove duplicate array 
Javascript :: javascript set element class 
Javascript :: javascript bool 
Javascript :: find object and remove in array 
Javascript :: how to separate string elements in javascript 
Javascript :: sequelize get data 
Javascript :: why we use mongoose 
Javascript :: image to base64 js 
Javascript :: js add zeros before number 
Javascript :: javascript foreach loop array 
Javascript :: prisma bigint 
Javascript :: vs code jsconfig 
Javascript :: react social login buttons 
Javascript :: shadow react native generator 
Javascript :: getelementbyclassname get multiple class 
Javascript :: Searchkick::ImportError: {"type"="cluster_block_exception" 
Javascript :: setting live reload sublime text 3 
Javascript :: math.floor javascript 
Javascript :: NodeJS router model 
Javascript :: javascript detect back space 
Javascript :: check if date is less than today moment 
Javascript :: useDebounce 
Javascript :: js check if string contains character 
Javascript :: examples of Conditional Operator js 
Javascript :: Get the language of a user 
Javascript :: check if an element is displayed jquery 
Javascript :: shorthand if in javascript with return 
Javascript :: how to check if expo app is running on the web 
Javascript :: how to trim the file name when length more than 10 in angular 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =