jquery - MVC - Why is Ajax call to Controller Action parameter always null? -


i making ajax call controller action using .get parameter null when controller action fired. trigger .on(change) event in jquery script contained in _layout.cshtml. posted question earlier, thread got trashed unrelated chatter. can me troubleshoot why happening? here code:

partial view: ~/views/shared/_languagelistpartial.cshtml

@html.dropdownlistfor(     x => x.selectedlanguage,     model.languages,     new { @class = "form-control" } ) 

_layout.cshtml

<form action="/account/listpartial" method="post"     class="navbar-form navbar-right nopadding" role="" style="margin: 8px 0;">     @{html.renderaction("languagelistpartial", "account");}  </form>  <div id="test"></div> 

get: /account/languagelistpartial

[allowanonymous] [childactiononly] public actionresult languagelistpartial() {     // retrieve user's language preference. default = en-us     string selectedlanguage = getlanguage();      var model = new languagelistpartialviewmodel();      model.languages = getlanguagelist();     model.selectedlanguage = selectedlanguage;      return partialview("_languagelistpartial", model); } 

get: /account/testpartial (test action .on(change) event)

[allowanonymous] public actionresult testpartial(string selectedlanguage) {     // set language selection in cookie , user profile     setlanguage("selectedlanguage");      var model = new languagelistpartialviewmodel();      // create list<selecteditemlist> of languages     model.languages = getlanguagelist();      // set user's language preference dropdown     model.selectedlanguage = selectedlanguage;      return view(model); } 

i haven't been able debug above code, dropdown renders expected:

<form method="post" action="/account/listpartial">     <select name="selectedlanguage" id="selectedlanguage">         <option value="en-us">english</option>         <option value="fr-ca">francais</option>         <option value="pt-br">portugues</option>         <option value="es-mx">espanol</option>     </select> </form>  <div id="test"></div> 

at bottom of _layout.cshtml have jquery:

@scripts.render("~/bundles/jquery") @scripts.render("~/bundles/jqueryui") @scripts.render("~/bundles/bootstrap") @rendersection("scripts", required: false)  <script type="text/javascript">     $(document).ready(function () {         $(function () {             $('#selectedlanguage').on('change', function () {                 var = $(this).val();                 $('#test').load("/account/testpartial/" + a);                 return false;             });         });     }); </script>  </body> </html> 

the problem $(this).val() == null no matter try. don't see wrong of code , hoped sharper eye mine might see wrong. i've tried ie, firefox , chrome. give same result. i've tried moving block partialview, controller action doesn't fired.

am violating rule of structure placement of jquery? have other jquery scripts in various pages , work, though 1 i've tried in layout.

one other weird thing reason script doesn't show in firefox debugging. don't think i've ever had problem before, don't show under scripts. although, it's getting executed since when make change in dropdown controller action being called.

edit #1

viewing in fiddler, see jquery working:

get http://localhost:59655/account/testpartial/pt-br http/1.1 x-requested-with: xmlhttprequest accept: text/html, */*; q=0.01 referer: http://localhost:59655/account/login?returnurl=%2f accept-language: en-us,en;q=0.5 accept-encoding: gzip, deflate user-agent: mozilla/5.0 (windows nt 6.3; wow64; trident/7.0; touch; rv:11.0) gecko host: localhost:59655 dnt: 1 connection: keep-alive cookie: __requestverificationtoken=8mk4-p6mrzcx6iw6dvdad70qvjenwubw6s0uxxpw7lxkawn7wzlxompoz59o9oq2get2a7shvyzxkmoojqnxgucshkyhqsnfcj62rhf2yia1 

even though pt-br being passed controller action testpartial, selectedlanguage null.

so @ point feel _layout.cshtml coding correct (including jquery). partial view code works , renders html. problem testpartial() don't have ideas why it's wrong. :s

edit #2

i found interesting article guy having same problem asp.net mvc. indicates problem happened him when controller action parameter named same 1 of model members. parameter named same related model property, renaming made no difference. article suggests me there may obscure reason why happening.

edit #3

as per suggestion @rune, changed parameter id testpartial(string id). didn't make difference id still null when called jquery script. have other controller actions have varying parameter names other id , work expect. when configure route suggested @rune error message saying testpartial.cshtml not found , showing of locations checked.

you need configure routes capture selected language. like

routes.maproute(                 name: "default",                 url: "{controller}/{action}/{selectedlanguage}",                 defaults: new { controller = "home", action = "index", selectedlanguage = "en-us" }             ); 

the central issue this: you have somehow tell asp.net mvc want last part of url associated selectedlanguage argument - how mvc ever guess on own?

alternatively, if have default route setup

routes.maproute(                 name: "default",                 url: "{controller}/{action}/{id}",                 defaults: new { controller = "home", action = "index", id = urlparameter.optional }             ); 

you have change action use id parameter

actionresult testpartial(string id) { } 

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -