0

I have a dropdown in my view to which i want to apply jquery(ajax onChange value). So that JSon data passed for the selected value by controller action rendered over the same view by replacing some id of that view
Dropdown

<div><select id="package_master" name="package_master"><option value="">
--select package--</option>
<option value="1">JPMC Package1</option>
<option value="2">JPMC Package 2</option>
<option value="3">JPMC Package1</option>
<option value="5">Select Package</option>
<option value="6">Select Package</option>
</select></div>


What i am presently doing is

<script type="text/javascript">
$(function () { //$(document).ready(function () {
    $("#package_master").change(function () {
        var value = $('#package_master').val();
        jQuery.ajax({
        type: "POST",

        url: "@Url.Content("~/Test/getPackageDetails/")",
        data: "packageID="+value,
       success: function(data){//here i want to Replace come id with json data passed by action
</script>

url is url: "@Url.Content("~/Test/getPackageDetails/")",

Complete Above script

Updated:
I used following script to accomplish my task which is working fine too, but data is displayed in array format like ["abc","xyz"]
Now here i need help to format json data in row format i.e 1 row for each array element like
abc
xyz

`

    <script type="text/javascript">
      $(function () { //$(document).ready(function () {
      $("#package_master").change(function () {
        var value = $('#package_master').val();
        jQuery.ajax({
        type: "POST",

        url: "@Url.Content("~/Test/getPackageDetails/")",
        data: "packageID="+value,

      success: function(data){

        $("#packageDetails").html(JSON.stringify(data));

        },
        error: function(data){
        alert("Request couldn't be processed. Please try again later. the reason "+data);
        }
        });

    });
});

`

4
  • Yup true . I tried a lot to explain my asked context but fails everytime Commented Dec 27, 2011 at 6:56
  • Is there any way to improve this? Commented Dec 27, 2011 at 6:56
  • 2
    Sure. Commonly, questions have a Question mark '?'. What is your question here? Commented Dec 27, 2011 at 6:58
  • Thanks roXon i'll take care of it in future. My question is to "complete above script" to replace some view id(say <div id="displayJsonData"/) with json data on sucess Commented Dec 27, 2011 at 7:01

4 Answers 4

2
<script>
    $(function () {
        $("#package_master").change(function () {
            var data = { packageID: $(this).val() }
            $.post("url", data, function (backdata, status) {
                // callback, do something
            }, "json");
        });
    });
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Nice and simple. +1. Just missing a semicolon in line 3
2
<script type="text/javascript">
$(function () { //$(document).ready(function () {
    $("#package_master").change(function () {
        var value = $('#package_master').val();
        jQuery.ajax({
        type: "POST",

        url: "@Url.Content("~/Test/getPackageDetails/")",
        data: "packageID="+value,
       success: function(data){
      data = $.map(data, function (item, a)
      {
      return " "+item+ "<br>";
      });
      $("#packageDetails").html(data.join(""));

       },

        error: function(data){
        alert("Request couldn't be processed. Please try again later. the reason        "+data);
        }
        });

    });
});

Comments

0

jQuery ajax function has an option: dataType: "json"

Since you are using JSON.stringify(data) you need this parameter as dataType: "text"

So there's no need for JSON.stringify(data).

1 Comment

try jQuery.ajax({ type: "POST",dataType: "json",.... }); without JSON.stringify
0
 $(function () { $(document).ready(function () {
    $("#package_master").change(function () {
        var packageID = $('#package_master').val();

        $.post("@Url.Content("~/Test/getPackageDetails/")", {packageID : packageID},         function (data) {
      $("#packageDetails").html("");
       $.each(data,function(i,field){
       $("#packageDetails").append(i+1+" "+field+"<br/>");
       });

        }, "json");
     }); 
   });

Alternatively you can use

$("#packageDetails").$(selector).load(url,data,function(response,status,xhr))

for other solutions, see here. And I'd recommend to avoid using $.ajax if you have some alt.

3 Comments

But doing this i'll append returned data to previous data(returned for previous value change) (Appends content to the HTML of selected elements). i.e if i change dropdown value twice then packageDetails i'll hold data for both value change. Need Solution
@dhiraj and gimme brk there always a better way to do things. My way to do this is :: add $("#packageDetails").html(""); this line just above each statement. This will provide empty packageDetails for each loop
additionally you can use replaceall() also

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.