0

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
}
12
  • Can you console.log(ajaxContext) for us? Commented May 6, 2014 at 8:09
  • Can you check with ActionResult instead of JsonResult? Commented May 6, 2014 at 8:13
  • Object { originalEvent=Event readystatechange, type="complete", timeStamp=1399364010574000, more...} It's many lines, what are you looking for? Commented May 6, 2014 at 8:14
  • Is complete function the jQuery.ajax complete? Commented May 6, 2014 at 8:15
  • szpic, it didn't help. Commented May 6, 2014 at 8:18

1 Answer 1

2

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);
}
Sign up to request clarification or add additional context in comments.

4 Comments

They JsonRequestBehavior.AllowGet is new for me, I will try that first.
You can refer to this answer for greater details.
your ajax call, uses type: "GET" and dataType: "json". I don't know what I use because I use the url action helper: @Url.Action . Where can I see? View sourcecode on the page and try found it? Also I should mention that I have the post attribute on my action [HttpPost]. I can't have that and use your "GET" at the same time.
From your question it seems like you are trying to do an AJAX get since you are returning a json object in your MVC Controller Action.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.