Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How do I call a post actionresponse passing the selected dropdown item as a parameter from javascript within a view within asp.net, razor, mvc

Basically, as I'm sure you can tell from the below code, I'm trying to accomplish the above without much success. What should I change to be successful? Thanks


C#
<script type="text/javascript">
$(function() {
    $("#mydropdown").change(function() {
        var selectedItem = $(this).val();
        $.ajax({
            url: '@Url.Action("DoStuff", "MainController")',
            type: "Post",
            data: { name: selectedItem },
            success: function () {
                alert('success');
            }
        });

    });
});

   [HttpPost]
    public ActionResult DoStuff(String Selecteditem)
    {
        CIModel CIModellList = CILHelper.ImportFunc(Selecteditem);
        return View(CIModellList);
    }


As of now, nothing happens whem the dropdown is changed. Although if I remove the ajax function, and replace this with an alert. Then the alert in this case does appear.

At this point, both the actionresult and the controller appear as though they do not exist (red) within the project even though both do and are named as stated.
Posted
Updated 20-Oct-14 3:04am
v5
Comments
Kornfeld Eliyahu Peter 20-Oct-14 8:48am    
Does it hits the server side at all?
ForumCrazy 20-Oct-14 9:08am    
The c# seems to suggest neither the action or the controller actually exist, giving the errors "cannot resolve action" or "cannot resolve controller"
Richard Deeming 20-Oct-14 9:15am    
Shouldn't the javascript property name match the name of the action parameter?
data: { SelectedItem: selectedItem }
Kornfeld Eliyahu Peter 20-Oct-14 9:17am    
Should - for mapping, but JavaScript will try to interpret SelectedItem as variable name and not as string a fail (mostly silently), so it should be data: { 'SelectedItem': selectedItem }
ForumCrazy 20-Oct-14 9:18am    
I have changed this also. But the JavaScript still suggests, neither the action-result or the controller exist even though they do

1 solution

I see a few problems with your code...
1.
The controller name probably Main and not MainController (it's true that the class implementing the controller called MainController, but the controller named Main only) - check it!
2.
JavaScript
data: { name: selectedItem }

I cant see where name variable declared, and I think you meant to call the varable 'name', so put it in quotes!
JavaScript
data: { 'name': selectedItem }
 
Share this answer
 
v2
Comments
Richard Deeming 20-Oct-14 9:19am    
The @Url.Action(...) syntax looks like a valid Razor declaration to me.
Kornfeld Eliyahu Peter 20-Oct-14 9:20am    
You are right - I missed the @ so I missed the Razor thing...Fixing it...Get a virtual 5 for that :-)
ForumCrazy 20-Oct-14 9:31am    
My controller is named, as wrote above. Also I have modified to look as follows data: { 'SelectedItem': selectedItem } but the javascript still suggests neither the action nor the controller exists.
Kornfeld Eliyahu Peter 20-Oct-14 9:43am    
Try
data: JSON.stringify({ 'Selecteditem': selectedItem })
(I see case difference between client and server - check it too)...
---
What are you saying that you have a controller like this?
public class MainControllerController : Controller

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900