I have a DropDownListFor that is in a Partial View. On change it fires a jQuery script, but Fiddler shows an HTTP 500 Error:
The action 'LanguageListPartial' is accessible only by a child request.
The calling script:
<script type="text/javascript">
$(function () {
$('#SelectedLanguage').on('change', function () {
var culture = $(this).val();
$('#test').load("/Account/LanguageListPartial/" + culture, function () {
location.reload(true);
});
});
});
</script>
I wouldn't want that Controller Action called directly so it is decorated with [ChildActionOnly]
. I realize that it's being called directly with the jQuery .load()
.
Is there a way to keep the ChildActionOnly
restriction and still call it from jQuery with the .on('change' ...)
event?
First : Create a Custom Attribute that supports both ChildActionOnly and Ajax Functionality:
public class AjaxChildActionOnlyAttribute : ActionMethodSelectorAttribute
{
public override bool IsValidForRequest(ControllerContext controllerContext, System.Reflection.MethodInfo methodInfo)
{
return controllerContext.RequestContext.HttpContext.Request.IsAjaxRequest() || controllerContext.IsChildAction;
}
}
Then : Use above created attribute for your partial View:
[AjaxChildActionOnly]
public PartialViewResult LanguageListPartial(string id)
{
//Function Logic
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With