I have a view containing model data. I want to pass a selection from a selection list while also passing the view data to the controller. I am new to Javascript and find it hard to get it working. After a lot of time searching for different solutions and examples, I found pieces which together made the javascript code below.

Selection box inside the view:

<select id="test" onchange="jsFunction()">
            <option value="pass">pass</option>
            <option value="fail">fail</option>
</select>

Javascript Code:

var e = document.getElementById("test") 
var selected = e.options[e.selectedIndex].value    
       function jsFunction() {    
            content = {
               ContentType: $("#ContentTypeID").val(),
           }
                   $.ajax({
                   url: '@Url.Action("Ajx_PassedResults", "Audi_B9_AisMura")',
                   type: 'POST',
                   contentType: 'application/json',
                   data: JSON.stringify(content),
                   success: function (result) {                
                   $('#divAisMura').replaceWith('<div style="overflow-x:scroll;width:100%" id="divAisMura">' + result +'</div>');                    
                   },
                   error: function (result) {
                       alert("something went wrong");
                   }
               });
            };

Controller Code:

public PartialViewResult Ajx_PassedResults(string selection)
        {
            //Clean the received string
            selection = selection.Replace("/", "").Replace("\"", "");
            //Retrieves the data
            List<object> viewDataList = TempData.Values.ToList();
            List<audi_b9_aismura> dataList = new List<audi_b9_aismura>();
            if (viewDataList != null)
            {
                foreach (audi_b9_aismura aismuraEntry in viewDataList)
                {
                    if (aismuraEntry.TestResult.ToLower().Contains(selection))
                    {
                        dataList.Add(aismuraEntry);
                    }
                }
            }
            //Returns the partial view
            return PartialView("_AisMura", dataList);
        }

Now, I got it working if I remove the parameter selection and hardcode .Contains("insertValueHere").

But I want to pass the selected value dynamically.

I have tried changing in the Javascript Code, 'data:' to:

data: {JSON.stringify(content), selection = selected },

But that does not work.

Related posts

Recent Viewed