Home Simple jquery ajax call to controller: what is causing 500 Internal server error?
Reply: 0

Simple jquery ajax call to controller: what is causing 500 Internal server error?

user1382
1#
user1382 Published in June 20, 2018, 1:27 am

I know that there are a lot of similar questions here on SO, but none of the ones I have read could help me solve my problem.

I have a drop down list with several items to choose from, the change of selection should trigger a method call in the controller OnItemSelectedChange(string itemName)

View (~/Home/Views/Index.cshtml)

@Html.DropDownListFor(model => model.SelectedItem, new 
SelectList(@Model.Items), new { onchange = "SelectedItemChanged(this);" })

jquery

function SelectedItemChanged(selectObj) {
        itemName = selectObj.value;
        $.ajax({
            type: 'POST',
            url: '/Home/OnSelectedItemChanged/?itemName=' + itemName,
        }).done(function () {
            alert('Modified item');
        });
    }

Controller's action (~/Controllers/HomeController)

[HttpPost]
public ActionResult OnSelectedItemChanged(string itemName)
{
    // do stuff 
    return View(new MyModel {...});
}

The javascript is being called fine, but POST Home/OnSelectedItemChanged?itemName=item3 returns "500 Internal Server Error".

I believe the problem is in the routing to the controller method, yet I thought MVC auto-populated the parameters

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.311648 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO