I have a DropDownListFor that is in a Partial View. On change it fires a jQuery script, but Fiddler shows an HTTP 500 Error:
我有一个部分视图中的DropDownListFor。在更改时它会触发jQuery脚本,但Fiddler显示HTTP 500错误:
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()
.
我不希望直接调用Controller Action,因此它使用[ChildActionOnly]进行修饰。我意识到它是用jQuery .load()直接调用的。
Is there a way to keep the ChildActionOnly
restriction and still call it from jQuery with the .on('change' ...)
event?
有没有办法保持ChildActionOnly限制,仍然使用.on('change'...)事件从jQuery调用它?
2 个解决方案
#1
6
No, you cant do that. The whole point of the ChildActionOnly
attribute is to prevent it being invoked as a result of a user request.
不,你不能这样做。 ChildActionOnly属性的重点是防止因用户请求而调用它。
#2
10
First : Create a Custom Attribute that supports both ChildActionOnly and Ajax Functionality:
第一步:创建一个支持ChildActionOnly和Ajax功能的自定义属性:
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
}
#1
6
No, you cant do that. The whole point of the ChildActionOnly
attribute is to prevent it being invoked as a result of a user request.
不,你不能这样做。 ChildActionOnly属性的重点是防止因用户请求而调用它。
#2
10
First : Create a Custom Attribute that supports both ChildActionOnly and Ajax Functionality:
第一步:创建一个支持ChildActionOnly和Ajax功能的自定义属性:
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
}