0

Hi i want to display the list of values in the view. I used ajax post function and i'm able to pass the values from the controller to view.

When i tried to access those values, it is not showing up.

Here is my model:

public IEnumerable<LoanStatusHistory> LoanHistory { get; set; }

My Controller:

public ActionResult ChangeTimeZone(int id, int loanId)
        {
            LoanInfoResultModel model = new LoanInfoResultModel();
            LoanIdentifier loanIdentifier = new LoanIdentifier(1, loanId);
            model.LoanHistory = LoanTrackerServices.GetLoanStatusHistory(loanIdentifier);           
            return Json(model.LoanHistory.ToList(), JsonRequestBehavior.AllowGet);
        }

Here is my view

  $.getJSON('@Url.Action("ChangeTimeZone", "RegistrationPage")', { "id": a, "loanId":'@Model.LoanId.LoanId' },
                function (data) {
                    alert("HI");
                    console.log(data);
                    for (var i in data) {
                        //alert(this.data.LoanActivityDate);
                        alert(data[i]);
                    }
                    //$.each(data, function () {
                    //    //alert(data[0].LoanActivityDate);
                    //    alert(data["LoanActivityDate"]);
                    //});
});

i was unable to get the values from the data. Can anyone help me out of this.

user2619542
  • 223
  • 1
  • 5
  • 11
  • Are you getting anything? is there any data in data? Are there any errors in your console? – Gobo Dec 18 '13 at 21:41
  • @Gobo there are no errors. I am getting object, but i want the values with in the object. – user2619542 Dec 18 '13 at 21:44
  • possible duplicate of [Cascading drop-downs in MVC 3 Razor view](http://stackoverflow.com/questions/4458970/cascading-drop-downs-in-mvc-3-razor-view) – rexcfnghk Dec 19 '13 at 01:25

1 Answers1

1

If an object is used as the collection, the callback is passed a key-value pair each time, so try below code:

$.getJSON('@Url.Action("ChangeTimeZone", "RegistrationPage")', { "id": a, "loanId": '@Model.LoanId.LoanId' },
   function (data) {
      $.each(data, function (key, item) {
       var sDate = ConvertJsonDateString(item.LoanActivityDate);
       var str = '<li>' + sDate + '</li>';
       $("#contents").append(str);
    });
});

Update You have json format Date, you need to covert to short Date, use below function.

    function ConvertJsonDateString(jsonDate) {
        var shortDate = null;
        if (jsonDate) {
            var regex = /-?\d+/;
            var matches = regex.exec(jsonDate);
            var dt = new Date(parseInt(matches[0]));
            var month = dt.getMonth() + 1;
            var monthString = month > 9 ? month : '0' + month;
            var day = dt.getDate();
            var dayString = day > 9 ? day : '0' + day;
            var year = dt.getFullYear();
            shortDate = monthString + '/' + dayString + '/' + year;
        }
        return shortDate;
    };

example:

<script>
    var jsonDate = '/Date(1375987087000)/';
    var date = ConvertJsonDateString(jsonDate);
    alert(date)  // the result will be 08/08/2013
</script>
Lin
  • 15,078
  • 4
  • 47
  • 49
  • It is not problem with the URL. I'm able to pass values like id and loanid to that method and getting the object which i returned. The only thing is i'm unable to get the values with in the object. @Lin – user2619542 Dec 18 '13 at 21:30
  • I'm getting a list of objects. Suppose my model.LoanHistory having a list of 10 items then it is displaying 10 objects like [Object : object]. This is the problem i'm having. i need to get the values with in the object. – user2619542 Dec 18 '13 at 21:57
  • I don't think we need to loop again because we are getting the 10 items(objects), but the thing is how to access that. i tried using data.LoanActivityDate and this.LoanActivityDate and it shown up nothing. – user2619542 Dec 18 '13 at 22:07
  • Are you sure you got the data from your action method? And what does your LoanStatusHistory model look like? – Lin Dec 18 '13 at 22:54
  • Yea i'm sure. I want to get the LoanActivityDate with in that object. I'm getting a string like this "/Date(1375987087000)/". when i converts it showing the current time, but not the original time – user2619542 Dec 18 '13 at 22:57
  • Your question was how to show the data, that's the answer I gave you. For showing the date format, you need to format your Json date to short date format. – Lin Dec 19 '13 at 01:30
  • hi @user2619542, I updated my answer to include date formatting. This should be what you want now. – Lin Dec 19 '13 at 01:50
  • hi @user2619542, if it's still not working, let me know. – Lin Dec 19 '13 at 16:32
  • I made that worked by converting all the datetimes to list of strings and pass that to the model. Because javascript don't know about the DateTime so it is just displaying a raw date. Thanks for your time and help @Lin – user2619542 Dec 19 '13 at 18:46