Json 无法从对controller razor的ajax调用中获取参数

Json 无法从对controller razor的ajax调用中获取参数,json,ajax,asp.net-mvc,razor,razor-pages,Json,Ajax,Asp.net Mvc,Razor,Razor Pages,简单用例-在输入选择(dropdownlist)值更改后,从AJAX调用动态获取PartialView以更新主页中的div 我采取的步骤: 使用@model ViewModelCreateOperation声明的模型创建视图(仅wihtout PageModel) 已在主页上创建复选框: 主要问题: 我的参数为空-我不知道为什么: 奖金问题: 我无法以任何方式调用我的控制器(尝试使用“~/meaningoflichangecontroller/getMeaningOfLife”或“/meani

简单用例-在输入选择(dropdownlist)值更改后,从AJAX调用动态获取PartialView以更新主页中的div

我采取的步骤:

  • 使用@model ViewModelCreateOperation声明的模型创建视图(仅wihtout PageModel)
  • 已在主页上创建复选框:
  • 主要问题: 我的参数为空-我不知道为什么:

    奖金问题: 我无法以任何方式调用我的控制器(尝试使用“~/meaningoflichangecontroller/getMeaningOfLife”或“/meaningoflichange/getMeaningOfLife”和“~/meaningoflichangecontroller/getMeaningOfLife”等组合),所以我在方法之前添加了[Route(“meaningofroutedname”)]和[HttpPost]。我不明白为什么。。。 在启动时,我添加了要初始化的控制器(JSON用于其他东西(API)):


    这不是我的答案,但是孟家栋在ASP.NET论坛上帮助了我。我正在发布他的答案:

    由于您想要发送的数据只是一个字符串类型的数据,因此需要像下面那样对其进行字符串化

    var dataToPost = JSON.stringify(value);
    
    然后在操作中,还应该添加[FromBody]属性

    public ActionResult getMeaningOfLife([FromBody]string operationName)
    
    namespace MyMysteriousApplication.Controllers
    {
        [Route("MeaningOfLifeRoutedName")]
        public class MeaningOfLifeChangesController : Controller
        {
            private readonly MyMysteriousApplication.Models.TTSCDBContext _context;
    
            public MeaningOfLifeChangesController(MyMysteriousApplication.Models.TTSCDBContext context)
            {
                _context = context;
            }
    
            public ViewModelCreateOperation viewModelCreateOperation { get; set; }
    
         
            public IActionResult Index()
            {
                return RedirectToPage("../Index");
            }
    
    
            [HttpPost]
            public ActionResult getMeaningOfLife(string operationName)
            {
                viewModelCreateOperation = new ViewModelCreateOperation();
    
    
                viewModelCreateOperation = new ViewModelCreateOperation();
                viewModelCreateOperation._entitiesSelectListItem = _context.Entities
                                                                      .Select(a => new Microsoft.AspNetCore.Mvc.Rendering.SelectListItem()
                                                                      {
                                                                          Value = a.Id.ToString(),
                                                                          Text = a.EntityName
                                                                      }).OrderByDescending(u => u.Text)
                                                                      .ToList();
    
    
    
                viewModelCreateOperation.MeaningOfLifeChanges = _context.MeaningOfLifeChanges.Where(u => u.OperationName.Contains(operationName)).OrderBy(u => u.ChangeId).FirstOrDefault();
    
    
    
                return PartialView("../projectManagement/partialViewCreateNewMOL", viewModelCreateOperation);
            }
    
    
        }
    
    }
    
    services.AddControllersWithViews().
             AddJsonOptions(options =>
             {
                 options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
                 options.JsonSerializerOptions.PropertyNamingPolicy = null;
                 options.JsonSerializerOptions.MaxDepth = 150;
             }).AddRazorRuntimeCompilation();
    
    var dataToPost = JSON.stringify(value);
    
    public ActionResult getMeaningOfLife([FromBody]string operationName)