0

i am sending a ajax request to a contrller to get some data.may be it working.but i cant use it in my project? dont know why ?means when i try to alert or use this data it not display .or even not giving me error message.

MAYBE THIS AJAX IS GOING TO DESTROY MY .NET INTEREST.

CONTROLLER CLASS/METHOD=>

[HttpPost]
public JsonResult Getdata(string batchcode)
{
   var studentid = (from a in db.Studentassignbatches
                    where a.batch_code == batchcode
                    select new { a.UserId });
   return Json(studentid,JsonRequestBehavior.AllowGet);
}

AND MY VIEW CODE AND AJAX =>

<script type="text/javascript">
function ShowBatchCode() {
    var batchcode = $('#BatchList').val();
    $.ajax({
        type: 'POST',
        dataType: 'json',
        contentType: 'application/json',
        url: '@Url.Action("Getdata", "Batche")',
        data: JSON.stringify({ batchcode: batchcode }),
        success: function (data) {
            var result = JSON.parse(data);
            result.forEach(function (obj) {
                alert(obj.UserId);
            });
        },
        error: function (result) {
            alert('Something Went Wrong!');
        }
   });
}
</script>

AND HTML=>

<select onchange="ShowBatchCode()" id="BatchList" class="form-control input-lg">
   <option class="pull-left" value="CCNA Security-1">CCNA Security-1</option>
   <option class="pull-left" value="CCNA Security-2">CCNA Security-2</option>
   <option class="pull-left" value="JNCSP-SEC-1">JNCSP-SEC-1</option>
   <option class="pull-left" value="Oracle Database 12c-1">Oracle Database 12c-1</option>
</select>

AND DATABASE=>

DATABASE AND MY bROWSER INSPECTION RESULT=>

bROWSER iNSPECTION HERE YOU CAN SEE THE ID IS COMING BUT WHY I CANT USE IT... ANY SUGGESSION OR HELP????

5
  • can you do console.log(data) in success callback of ajax? Commented May 7, 2017 at 16:27
  • let me try..... Commented May 7, 2017 at 16:31
  • why do you use JSON.parse(data)? I think the data is already a JSON object. try alert(data.UserId) in the success callback. Commented May 7, 2017 at 16:37
  • alert(data.UserId) saying undefined! Commented May 7, 2017 at 16:39
  • you already have JSON object. so simply use the data. Check my answer Commented May 7, 2017 at 16:41

1 Answer 1

1

No Need to JSON parse as it is already JSON object

<script type="text/javascript">
function ShowBatchCode() {
    var batchcode = $('#BatchList').val();
    $.ajax({
        type: 'POST',
        dataType: 'json',
        contentType: 'application/json',
        url: '@Url.Action("Getdata", "Batche")',
        data: JSON.stringify({ batchcode: batchcode }),
        success: function (data) {
            data.forEach(function (obj) {
                alert(obj.UserId);
            });
        },
        error: function (result) {
            alert('Something Went Wrong!');
        }
   });
}
</script>
Sign up to request clarification or add additional context in comments.

Comments

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.