0

i have developed a web application using mvc4.i need to pass some <IList> from controller to view as json result.

below is the code in controller class

public ActionResult GetTradeUserData([DataSourceRequest] DataSourceRequest request, int test) 
        {
            wholeSaleModelUser = new WholeSaleInfoService().PopulateWholeSaleUserData(test);
            return Json(wholeSaleModelUser, JsonRequestBehavior.AllowGet);
        }

and here is the code of view class

<script>
$("#submitMarketUser").click(function () {
    $.ajax({
        url: "WholeSaleTrade/GetTradeUserData",
        data: { test: $("#Names").val() },
        dataType: "json",
        type: "POST",
        success: function (data) {

            alert(data.EmpNm);

            $("#Contact").val(data.Contact);
            $("#EPFNo").val(data.EPFNo);
            $("#TitlKy").val(data.TitlKy);
            $("#EmpNm").val(data.EmpNm);
            $("#NameInInitials").val(data.NameInInitials);
            $("#DtBirth").val(dateFromStringWithTime(data.DtBirth));
        },
        error: function (e) {
            return false;
        }
    });
});

my problem is data object not getting any data and alert popup with a text as "undefined".

can somebody please help me here.

10
  • 1
    Is the method GetTradeUserData marked with the [HttpPost] attribute? The method won't be hit otherwise as it will default to accepting GET requests and you're POSTing. Furthermore, did you add a breakpoint and see if your code was actually hit? Commented Jun 25, 2013 at 7:17
  • What does alert(JSON.stringify(data)); show in your success callback? Commented Jun 25, 2013 at 7:24
  • What browser are you using? Commented Jun 25, 2013 at 7:25
  • @steinar yes of course.. return Json(wholeSaleModelUser, JsonRequestBehavior.AllowGet); in here wholeSaleModelUser get the values as code hit Commented Jun 25, 2013 at 7:26
  • @Andrei google chrome Commented Jun 25, 2013 at 7:28

2 Answers 2

1

Alright, now that you have at last shown the JSON sent by your service it is clear what the problem is. You've got a collection of users and not a single element:

[{"Name":"MarketFarmer","Contact":777369369,"Email":"[email protected]","Fax":114652652,"Address":"kalubowila road,dehivala"}]

Also please not that the valid properties of this object are Name, Contact, Email, Fax and Address but in your code you are attempting to read some EPFNo, TitlKy, EmpNo, NameInInitials and DtBirth which do not exist in your returned object

So in order to access its values you need to get the element inside the array and then you can access the available properties:

success: function (data) {
    var employee = data[0];

    alert(employee.Name);
    alert(employee.Contact);
    alert(employee.Email);
},
Sign up to request clarification or add additional context in comments.

2 Comments

Thanx alot sir.! finally i solved the problem with your answer.
By the way my final step is i need to assign those values to textboxes which exist in a different view(not in the view this script running) one by one.how to do that?how to pass those values to a partial view or another view???
1

Maybe you should change ActionResult to JsonResult in your controller

public JsonResult GetTradeUserData([DataSourceRequest] DataSourceRequest request, int test) 
        {
            wholeSaleModelUser = new WholeSaleInfoService().PopulateWholeSaleUserData(test);
            return this.Json(wholeSaleModelUser, JsonRequestBehavior.AllowGet);
        }

3 Comments

i did the change but i couldn't get a positive answer.problem is still there.
Have you test if your wholeSaleModelUser variable is not null? Did you add a breakpoint in your controller method?
yes i checked with a breakpoint and wholeSaleModelUser variable getting approprate result but wont get them in view class

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.