我通过ajax jquery将多个对象发布到MVC 4控制器时遇到问题.已经好几周了,但我似乎无法找到解决方案.我尝试了几种方法,有时filterModel对象为null,有时字符串参数为null(即使我指定contentType或不指定contentType也无关紧要)
我想要的是?我想传递三个对象:1.filterModel 2.testparamA 3.testparamB我应该怎么做才能将所有三个对象传递给MVC控制器?我需要在数据中写什么:所以我得到所有3个对象值?
最简单的控制器
[HttpPost]
public JsonResult Test(string testparamA, string testparamB, FilterModel filter)
{
using (RBSystemEntities repository = new RBSystemEntities())
{
return Json(new {
DataList = repository.Items.Select(x => new {x.PKID, x.ItemName}).ToList(),
Result = "OK"
});
}
}
Run Code Online (Sandbox Code Playgroud)
最简单的观点
var filterModel = @Html.Raw(Json.Encode(new FilterModel("ItemName", "Pepperoni Pizza")))
//filterModel = JSON.stringify(filterModel);
function testme() {
// post the javascript variable back to the controller
$.ajax({
url: '/Menu/Test',
type: 'POST',
//contentType: 'application/json; charset=utf-8',
data: {
filter: filterModel,
testparamA: 'A value',
testparamB: 'B …Run Code Online (Sandbox Code Playgroud) 有没有办法覆盖AllowAnonymous属性?我已经实现了从数据库加载用户菜单和按钮的自定义授权,如下所示:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new MyCustomAuthorization()); // Custom Authorization for Rights & Priveleges
}
Run Code Online (Sandbox Code Playgroud)
以上工作正常.
现在,我希望在用户通过身份验证时允许访问某些操作,在这种情况下无需检查授权.例:
[Authorize]
public class MenusAndButtonsController : BaseController
{
[Authenticated] // my custom attribute that will check if user is logged in or not
public JsonResult GetGeneralMenuAndButtons()
{
using (MealPlannerAuthorizationEntities repository = new MealPlannerAuthorizationEntities())
{
var MenusAndButtons = repository.MP_AUTH_Menus.Where(x => x.IsButton == false && x.IsListButton == false).Select(c => new { DisplayText = c.MenuName, Value = c.MenuId }).OrderBy(x => x.DisplayText).ToList();
return Json(new …Run Code Online (Sandbox Code Playgroud) authentication custom-attributes filterattribute asp.net-mvc-4