0

when i use this code i get internal server error :

<script>
    function LoadRegion() {
        var countryId = document.getElementById("country").value;

        $.ajax({
            type: 'POST',
            url: "../Account/Register",
            data: $('#form').serialize(),                              
            dataType: 'json'                    
        });
    }
</script>

My question is : how can i pass this value in controller in specific field?

Controller:

[HttpPost]
public ActionResult Register(IndexPageModel model)
{
    model.Register.Country = new SelectList(manager.GetCountries(), "Id", "Name");  
    //I need to put here  SelectCountryId->model.Register.Region = new SelectList(manager.GetRegions(model.Register.SelectCountryId), "Id", "Name");

    return View(model);
}

View:

@Html.DropDownListFor(m => m.Register.SelectCountryId,
                      Model.Register.Country, 
                      "Select country", 
                      new { id = "country", 
                            @class = "form-control",
                            @onchange ="LoadRegion();"
                          }
                      )
ekad
  • 14,436
  • 26
  • 44
  • 46
None
  • 8,817
  • 26
  • 96
  • 171
  • You specify the return type as `json` but you return `html` - hence the error. What are you actually trying to do? –  May 27 '15 at 08:58
  • I have two dropdown lists and what im trying to do is when i change one dropdown list to get data in another dropdown but for now its working only when i hit submit button...i need to get data on second dropdown after i choose first one – None May 27 '15 at 09:00
  • I posted a link in your last question showing you how to do this. Which bit do you nor understand? –  May 27 '15 at 09:01
  • i dont know where to start...i always get internal error i cant get into post of controller – None May 27 '15 at 09:02
  • 1
    Look at the question and my answer in the previous link I gave you. You need to handle the `.change()` event of the first dropdown, pass its selected value to a method that returns json and then update the DOM by adding ` –  May 27 '15 at 09:05
  • when i choose data in first dropdown list and hit submit button i get data in second dropdown list but i dont know how to do it without submit button ...can u help me pls? – None May 27 '15 at 09:07
  • 1
    Study the link I gave you! –  May 27 '15 at 09:07
  • I tried but i still dont know how to update DOM by adding – None May 27 '15 at 09:09
  • http://stackoverflow.com/questions/740195/adding-options-to-a-select-using-jquery-javascript – Luke May 27 '15 at 09:10
  • @Coulton is this with fixed data? I populate second dropdown list dynamiclly – None May 27 '15 at 09:11
  • Loop through your dynamic data – Luke May 27 '15 at 09:12
  • how to loop through data? – None May 27 '15 at 09:14
  • Post the code that you have already where your dynamic data comes back – Luke May 27 '15 at 09:18
  • I posted all that i already have – None May 27 '15 at 09:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/78874/discussion-between-coulton-and-none). – Luke May 27 '15 at 09:22

1 Answers1

1

After chatting about this issue, we came up with the following solution:

You need to create an action method that returns just the values that you want to bring back. In this instance it is a list of Regions so that you can populate a select list.

public JsonResult GetCities(int countryId) 
{ 
    IUserManager manager = UserFactory.GetUserManager(WebConfiguration.TerminalId);
    var model = manager.GetRegions(countryId); 
    return Json(model); 
}

In your JavaScript you need to make the request to this action method and add the options that you want to add to the select list:

function LoadRegion() { 
    var cities = $('#CitiesSelectBox'); 
    var url = "/Account/GetCities";
    $.getJSON(url, { countryId: $('#CountryID').val() }, function (response) {     
    cities.empty().append($('<option></option>').val('').text('Please select')); 

    for (var i = 0; i < response.length; i++) { 
        cities.append($('<option></option>').val(response[i].Id).text(response[i].Name)); 
        } 

    }); 
}

The server error that you are getting is because you need to allow get requests on the JSON data. This isn't enabled by default:

// Enable GET requests on JSON response
return Json(model, JsonRequestBehavior.AllowGet)
Luke
  • 22,826
  • 31
  • 110
  • 193