0

I am trying to allow user to choose a sort method from a dropdown list.I am not fully certain how to accomplish this. Here are my beginnings.

RestaurantVm.cs:

I am trying to allow user to choose a sort method from a dropdown list.I am not fully certain how to accomplish this. Here are my beginnings.

RestaurantVm.cs:

// Not sure how populate this
public SelectList SortMethods { get; set; }
public string SelectedMethod { get; set; }

RestaurantsController:

public ActionResult sortedRestaurants( RestaurantVm  mv   )
{
    string SelectedValue = mv.SelectedMethod;
    switch (SelectedValue)
    {
        case "Name":
            SortByName()
        default:
            break;
    }
    return View(mv);
}

In the view:

@Html.DropDownListFor(m => m.SelectedMethod, Model.SortMethods, "Select a method")

Jquery Code:

$(document).ready(function(){
    $("#ddlId").change(function(){
        $.ajax({
            type:'POST',
            url:'url.action('Controller Name','Action Method Name')'
            data:{id:$('#ddlId').val()},
            sucess:{

            }
        });
    })
})

The problem:

I don't know how to populate selectList properly, I am not sure how to properly use jquery method to call action.

Leonardo Henriques
  • 784
  • 1
  • 7
  • 22
Curious-programmer
  • 772
  • 3
  • 13
  • 31
  • You can't call action in browser. Your asp code runs on server only...long before page is ever sent to browser – charlietfl May 06 '18 at 02:50
  • Not sure what you mean, why cant I call an action on dropdown change – Curious-programmer May 06 '18 at 02:54
  • [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – charlietfl May 06 '18 at 02:55
  • How would I accomplish this? – Curious-programmer May 06 '18 at 03:04
  • Not an asp guru but seems simple enough to send an action data param (from select value) and use same controller path for all the requests. Then just manage sorting based on the submitted action value – charlietfl May 06 '18 at 03:05
  • 1
    What are the elements with `id="ddlId"` and `id="ddlMake"`? What is the method you want to call? What do you want to do when you call the method? What the are options you want to display? –  May 06 '18 at 03:09
  • 1
    Suggest you start by looking at the code in [this answer](https://stackoverflow.com/questions/29142422/rendering-partial-view-on-button-click-in-asp-net-mvc/29142790#29142790) –  May 06 '18 at 03:14
  • 1
    It is OK to not know all the technical details of how to implement a solution for your problem. But you should spend a few minutes to write clear and concise "expected behavior" so anyone from the other side of the world should be able to understand what you are trying to do, without looking at your computer. Take a moment to read this page [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). A lot of people are here to help :) – Shyju May 06 '18 at 04:42
  • A generic answer to your problem : Listen to the change event on the dropdown and read the selected option value, You can do a GET call to the GET action method which returns the list of items , while passing the selected value in querystring (Ex : `yourSite/restaurants/list?sort=name`) and in your GET action method, you can read this parameter value and use that to do the sort. You do not necessarily need ajax to do this. If you want to update the results without reloading the page, then only you need to use ajax. – Shyju May 06 '18 at 04:45

0 Answers0