Wednesday, 18 October 2017

MVC - How to pass value from Controller to View using JSon


Watch this example on YouTube

 

Controller

       public JsonResult GetSomeValue()
        {
            return Json(new
            {
                SomeValue = "here is my value"
            });
        }

View - HTML

<input type="submit" value="Display Some Value" id="btnJson" />

View JavaScript


@section Scripts{
    <script type="text/javascript">
        $(document).ready(function () {
            $("#btnJson").click(function (e) {
                var val = {};
                val.url='@Url.Action("GetSomeValue", "Home")';
                val.type= "POST";
                val.dataType = "json";
                val.contentType = "application/json";
                val.success = function(response){
                    alert(response.SomeValue);
                };
                $.ajax(val);
            });
        });
    </script>
}

No comments:

Post a Comment