0

Well I want to pass two parameters using Json to the controller, where i use a query to get result....

Now I manage to pass one parameter(first one) but the second just wont work, if i did Price/4 is will work but adding /2 says 404 in console...

//Passing ID 4 and ID 3 to HomeController/Price
$(document).ready(function () {
  $("#type, #States").change(function () {
      $.getJSON('/Home/Price/4/2', function (data) {
          var items = '<option>Select a State</option>';
          $.each(data, function (i, state) {
             items += "<option value='" + state.Value + "'>" + state.Text +"</option>";
          });
          $("#Price").html(items);
      });
  });
});

//Home Controller Function

public JsonResult States(int id, int ids)
    {
        using (TalyllynContext db = new TalyllynContext())
        {
            var query = from F in db.Fares
                        join p in db.TicketPreferences on
                        F.TicketPreferenceID equals p.ID
                        where F.TicketTypeID == id &&
                        F.TicketPreferenceID == ids
                        select p;

            return Json(new SelectList(query.ToList(), "ID", "Name"),
            JsonRequestBehavior.AllowGet);
        }
    }

Any more just let me know....

3
  • Have you defined a route parameter for /Home/Price/{id}/{ids}? Or you can use $.getJSON('/Home/Price', { id: 4, ids: 2 }, function ... Commented Jan 16, 2015 at 12:12
  • AHHHH.... Yeah I have but your solution helped an work how it should, life saver thank you ! Commented Jan 16, 2015 at 12:17
  • Note also you do not need the extra overhead of .ToList() or creating a SelectList just use var query = from ...select p => new { Value = p.ID, Name = p.Name }); return Json(query, JsonRequestBehavior.AllowGet); Commented Jan 16, 2015 at 12:32

1 Answer 1

1

You should define such behavior in the route or use the query parameters. I've improved JavaScript code with tips from Stephen Muecke.

Query parameters approach:

$(document).ready(function () {
  $("#type, #States").change(function () {
//It's better to use Url.Action helper to construct your urls,
//So that they always will be in sync with your routes
      $.getJSON('@Url.Action("Price", "Home", new {id=4, ids=2})', function (data) {              
          var price = $("#Price");
          price.append($('<option></option>').val('').text('Select a State'));
          $.each(data, function (i, state) {
             price.append($('<option></option>').val(state.value).text(state.text))                 
          });
      });
  });
});

Or as stated by Stephen Muecke:

$(document).ready(function () {
  $("#type, #States").change(function () {
      $.getJSON('@Url.Action("Price", "Home")', {id: 2, ids: 3}, function (data) {
          var price = $("#Price");
          price.append($('<option></option>').val('').text('Select a State'));
          $.each(data, function (i, state) {
             price.append($('<option></option>').val(state.value).text(state.text))                 
          });
      });
  });
});

Route approach:

routed.MapRoute("Name", "{controller}/{action}/{id}/{ids}",
new {controller = "Home", action = "Index", ids=UrlParameter.Optional});
Sign up to request clarification or add additional context in comments.

5 Comments

Few other things to note: Don't hard code the url, use '@Url.Action("Price", "Home")'. The 'empty' option needs a value otherwise it will post back "Select a State" so use <option value>Select a State</option> or better $('#Price').append($('<option></option>').val('').text('Select a State')); and finally you probably want $('#Price').empty(); before the ajax call.
Agree with your points. Though, if this code is in js file the Url.Action will not work.
In that case you pass it as a parameter to the function
Yep, there're workarounds for this (we usually use data-* attributes), nice point though, I will include them in the answer
Thanks guys helped me allot and made it allot smarter !

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.