radbyx
radbyx

Reputation: 9650

How to access JSON property from MVC controller

Calling the Controller

action: @Url.Action( "UploadFiles",  "Dokument",  new {  } )

Building JSON object with startTabIndex

public JsonResult UploadFiles()
{
    var foo = 0;
    return Json(new { startTabIndex = foo });
}

How can I access the startTabIndexproperty?

complete: function (ajaxContext) {
    console.log('ajaxContext: ' + ajaxContext); // not undefined
    console.log(ajaxContext.startTabIndex); // undefined
    startTabIndex = ajaxContext.startTabIndex; // not working
}

Upvotes: 0

Views: 547

Answers (1)

chridam
chridam

Reputation: 103305

You could try putting this logic in a separate javascript file that could be referenced from your view. For example, you could store the url into a global javascript variable in the view:

<script type="text/javascript">
    var uploadFileUrl = '@Url.Action("UploadFiles", "FileController")';
</script>
<script type="text/javascript" src="~/scripts/myscript.js"></script>

and inside the script make the AJAX call:

$.ajax({
    type: "GET",
    dataType: "json",
    url: uploadFileUrl,
    success: function(data) {
        console.log(data);
    }
});

Your controller action you are invoking that returns a JsonResult:

public ActionResult UploadFiles()
{
    var foo = 0;
    var model = new
    {
        startTabIndex = foo
    };
    return Json(model, JsonRequestBehavior.AllowGet);
}

Upvotes: 2

Related Questions