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
}
radbyx
  • 9,352
  • 21
  • 84
  • 127
  • Can you `console.log(ajaxContext)` for us? – Cerbrus May 06 '14 at 08:09
  • Can you check with `ActionResult` instead of `JsonResult`? – szpic May 06 '14 at 08:13
  • Object { originalEvent=Event readystatechange, type="complete", timeStamp=1399364010574000, more...} It's many lines, what are you looking for? – radbyx May 06 '14 at 08:14
  • Is `complete` function the [jQuery.ajax](https://api.jquery.com/jQuery.ajax/) complete? – Zabavsky May 06 '14 at 08:15
  • szpic, it didn't help. – radbyx May 06 '14 at 08:18
  • Hm, and `console.log(arguments)` in the `complete` callback? – Cerbrus May 06 '14 at 08:19
  • My response from Firefox's firebug: "{"action_success":true,"startTabIndex":0}" and I have the same in Firefox's JSON tap: action_success true startTabIndex 0 – radbyx May 06 '14 at 08:20
  • Cerbrus: I am not sure I can paste and show you anything meaningsfull? What are we looking for? This is what I got: [Object { originalEvent=Event readystatechange, type="complete", timeStamp=1399364523919000, more...}, Object[input#wijUpload_1399364518744_input0 property value = "C:\Develop_docs...dekn.doc" attribute value = "null"]] – radbyx May 06 '14 at 08:25
  • I'm trying to find where the actual data went. Does `ajaxContent` contain a `data` property? – Cerbrus May 06 '14 at 08:26
  • ajaxContext.data is undefined. – radbyx May 06 '14 at 08:28
  • Do I have to tell it that I what the returned datatype to be JSON? Maybe I forgot that? – radbyx May 06 '14 at 08:32
  • Zabavski: No `complete`is from wijupload: var upload = $('foo').wijupload( complete: function (ajaxContext) { }) – radbyx May 06 '14 at 08:47

1 Answers1

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);
}
chridam
  • 100,957
  • 23
  • 236
  • 235
  • They `JsonRequestBehavior.AllowGet` is new for me, I will try that first. – radbyx May 06 '14 at 08:51
  • You can refer to this [answer](http://stackoverflow.com/a/8464685) for greater details. – chridam May 06 '14 at 09:01
  • 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. – radbyx May 06 '14 at 09:17
  • 1
    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. – chridam May 06 '14 at 10:04