Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

View Component does not POST/GET to action method

$
0
0

I have a viewcomponent in my _Layout Page as follows:

<form id="lang_form" asp-controller="Home" asp-action="RedirectToLanguage" method="get"><select name="lang" id="lang_ddl" asp-items="@ViewBag.LangItems"></select></form><script type="text/javascript">$(document).ready(function () {$("#lang_ddl").change(function (e) {$('#lang_form').trigger('submit');
        });

    });

</script>

That should send the form to an action method named: "RedirectToLanguage" if I use Post the form never submit and I get 404 but if the method is Get it goes to Index action method and tries to load it and its layout page again. the work of the "RedirectToLanguage" is to request the index action method with a new lang parameter, but when I place a debug mark to this action method it never executes. my take is the view component's form does not send to its target, "RedirectToLanguage". Is it related to viewcomponents or it is my own mistake?  I had  no problems with child actions in such scenarios. and in general is it possible to post/get a form from viewcomponent's view to an action method?

thanks.

Edit:

This is the RedirectToLanguage action method:

 [HttpGet]
        public ViewResult Index(string lang)
        {
            ViewBag.Lang = lang;
            return View();
        }

        [HttpGet]
        public IActionResult RedirectToLanguage(string lang)
        {
            return RedirectToAction("Index", "Home", new { lang = lang });
        }


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>