Search code examples
javascriptc#model-view-controllercontrollerdataservice

C#-MVC-JS using URL action to controller to service to return a string from a database


I'm attempting to pull in a value from a Database when a dropdown selection on a form is selected. So far, I've got the function being called when the Dropdown option is selected, the JS function sending the parameter to the Controller and the Controller sending it to the Service. I had it sending to the database but not returning data, but now it's not compiling at all due to type errors between the controller, the service and what I want to do. I'm gonna put the code I have here and then explain more about the issues:

View JS Function

 $("#FormInput_selector").change(
                function GetOtherValue(event) {

                    var selection = $(this).find('option:selected').val();

                   
             var url = '@Url.Action("GetOtherValue", "Operations")'                    
                 + "?value=" + selection;

                    $.get(url, function (data) {
                        console.log(data);
                       
                    });

                       
                })

This is operational. It is called and runs all the way through.

Controller

public ActionResult GetOtherValue(string value)
        {
            DataService db = new DataService();
          
           return db.GetSECMarket(value);
            
        }

Service

  internal ActionResult GetOtherValue(string value)
        {
            using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(ConnectionString_DEV))
            {
                return connection.Query<ActionResult>("dbo.StoredProcedureToGetValue @Value", new
                {
                    @Value = value
                });
            }
        }

Where have I gone wrong? I've tried adjusting the class from ActionResult to the Model I used in the View I'm calling from or to 'string'. I've also tried adjusting the class attached to 'Query.' in the same manner, and I keep getting "Cannot implicitly convert type" errors amongst others. Is my process fundamentally flawed or am I just missing something along the way?


Solution

  • Looks like you want to get a ActionResult? If you want to get JSON result in JS function , try this code.

    GetOtherValue

    public ActionResult GetOtherValue(string value)
    {
        DataService db = new DataService();
        var result = db.GetSECMarket(value);
        return Json(result, JsonRequestBehavior.AllowGet);
    }
    

    Create a model to map your internal data

    public class DropDownData
    {
        public int ID { get; set; }
        public string IDNAME { get; set; }
    }
    
    internal IEnumerable<DropDownData> GetSECMarket(string value)
    {
        using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(ConnectionString_DEV))
        {
            return connection.Query<DropDownData>("dbo.StoredProcedureToGetValue", new
            {
                Value = value
            });
        }
    }
    

    Hope this work.