0

I am trying to send back a categoryId and update the page with a new strongly typed object. For some reason, the cateogryId is coming in as null...

$(function () {
    $('#hhh').change(function () {
        var url = $(this).data('url');
        var categoryId = $(this).val();
        $.ajax({
            url: url,
            type: 'GET',
            cache: false,
            data: { categoryId: categoryId },
            success: function (result) {
                // TODO: manipulate the result returned from the controller action
            }
        });
    });
});

<h2>Index</h2>
@Html.ActionLink("Create New Listing", "Create")
<br/>
<strong>Filter Listings</strong>
@Html.DropDownListFor(
    m => m.SelectedCategoryGuid, 
    Model.Categories, 
    "Select a Category", 
    new {
        id = "hhh",
        data_url = Url.Action("SortListing", "Listing") 
    }
)

Controller Code:

public ActionResult SortListing(string categoryGuid)
    {
        var listingCategory = new ListingCategory();
        listingCategory.Id = _tourismAdminService.GetByGuid<ListingCategory>(Guid.Parse(categoryGuid)).Id;
        var listings = new List<Listing>();

        foreach (var listing in _tourismAdminService.ListAllEntities<Listing>())
        {
            if (listing.CategoryId == listingCategory.Id)
            {
                listings.Add(listing);
            }
        }

        return RedirectToAction("Index", "Listing", listings);
    }
2
  • Please post the action method from your Controller. Commented Sep 18, 2012 at 19:10
  • Hi. I added the code for the controller Commented Sep 18, 2012 at 19:11

2 Answers 2

2

Ahh, that helps. Change your javascript to this:

$(function () {
    $('#hhh').change(function () {
        var url = $(this).data('url');
        var categoryId = $(this).val();
        $.ajax({
            url: url,
            type: 'GET',
            cache: false,
            data: { categoryGuid: categoryId },
            success: function (result) {
                // TODO: manipulate the result returned from the controller action
            }
        });
    });
});

Notice I changed the data: {...} line in your javascript. You want to match your parameter names in the data line with the parameters in your Action method for the binding to work.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi. that worked. Got one last problem though but i'll make a new question for that I guess.
1

Your action method is expecting a parameter named categoryGuid but you are passing a parameter with a different name from your ajax code. in your javascript code, Change

 data: { categoryId: categoryId },

to

data: { categoryGuid: categoryId },

1 Comment

Thank you! that worked perfectly like Gomer's answer. Got a new problem though but i'll post it as a new question

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.