jQuery Ajax调用传递的参数在Asp.net MVC Core中始终为null [英] Jquery Ajax call passed parameter always null in Asp.net MVC Core

查看:60
本文介绍了jQuery Ajax调用传递的参数在Asp.net MVC Core中始终为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将MVC应用程序迁移到Asp.Net core(3.1版本)应用程序.我有一个用于菜单导航的布局页面.当用户单击任何菜单时,出于某些业务目的,我需要将一些值从Layout传递到控制器.为此,我在这里使用了一个ajax调用.

下面的Ajax调用代码在MVC中工作正常,但是在Asp.Net核心中,传递的参数值在控制器中为空.

MVC的工作代码:

 函数SetCurrentPageNameSession(currentPageName,isBookMarkPage){如果(isBookMarkPage == undefined)isBookMarkPage = false;var url = baseUrl +"Manage/HighlightCurrentMenu/"$ .ajax({输入:"POST",数据:"{'CurrentPage':'" + currentPageName +','IsBookMark':'" + isBookMarkPage +'}",网址:url,contentType:"application/json; charset = utf-8",dataType:"json",异步:假,成功:功能(数据){var res = data.d;},错误:函数(xhr,ajaxOptions,thrownError){}});}  

及以下是控制器操作方法,在这些方法中,我获得了这些传递的值:

  [HttpPost]公共IActionResult HighlightCurrentMenu(string CurrentPage,bool IsBookMark){返回Json(true);}  

当相同的代码在Asp.Net核心中不起作用时,我用Google对其进行了搜索,发现需要修改ajax调用代码.

我在下面修改了数据部分:

 函数SetCurrentPageNameSession(CurrentPage,IsBookMark){var url = baseUrl +"Manage/HighlightCurrentMenu/"$ .ajax({输入:"POST",数据:JSON.stringify({CurrentPage:CurrentPage,IsBookMark:IsBookMark}),网址:url,contentType:"application/json; charset = utf-8",dataType:"json",异步:假,成功:功能(数据){var res = data.d;},错误:函数(xhr,ajaxOptions,thrownError){}});}  

并在控制器操作方法中使用FromBody:

  [HttpPost]公共IActionResult HighlightCurrentMenu([FromBody]字符串CurrentPage,[FromBody] bool IsBookMark){返回Json(true);}  

我在"CurrentPage"中获得了null值;参数.

我在下面自己尝试了另一种方案:

 数据:JSON.stringify("{'CurrentPage':'" + CurrentPage +','IsBookMark':'" + IsBookMark +'}"), 

在这种情况下,我在"CurrentPage"中获得了完整的Json格式值.动作方法中的参数

下面是相同的屏幕截图.

请提出建议.

解决方案

在Asp.Net核心中,传递的参数值在控制器中为空

来自

I am migrating my MVC application to Asp.Net core (3.1 version) application. I have one layout page which is used for menu navigation. When user click on any menu then i need to pass some value from Layout to the controller for some business purpose. To do that i used one ajax call here.

Below ajax call code is working fine in MVC, But in Asp.Net core the passed parameter value is null in controller.

Working code of MVC:

function SetCurrentPageNameSession(currentPageName, isBookMarkPage) {

        if(isBookMarkPage==undefined)
            isBookMarkPage = false;
        var url = baseUrl+"Manage/HighlightCurrentMenu/"
            $.ajax({
            type: "POST",
            data: "{'CurrentPage':'" + currentPageName + "', 'IsBookMark':'" + isBookMarkPage + "'}",
                url: url,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async:false,
                success: function (data) {
                var res = data.d;
            },
                error: function (xhr, ajaxOptions, thrownError) {

            }
        });
    }

and below is the controller action method where i get these passed value:

[HttpPost]
        public IActionResult HighlightCurrentMenu(string CurrentPage, bool IsBookMark)
        {
           return Json(true);
        }

When the same code not works in Asp.Net core then i googled it and found like need to modify the ajax call code.

I modified data section in below:

function SetCurrentPageNameSession(CurrentPage, IsBookMark) {

        var url = baseUrl+"Manage/HighlightCurrentMenu/"
            $.ajax({
            type: "POST",
            data: JSON.stringify({ CurrentPage: CurrentPage, IsBookMark: IsBookMark }),
                url: url,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async:false,
                success: function (data) {
                var res = data.d;
            },
                error: function (xhr, ajaxOptions, thrownError) {

            }
        });
    }

And used FromBody in controller action method:

[HttpPost]
        public IActionResult HighlightCurrentMenu([FromBody] string CurrentPage, [FromBody] bool IsBookMark)
        {
          return Json(true);  
        }

I get the value null in "CurrentPage" Parameter.

I tried one more scenario by myself in below:

data: JSON.stringify("{'CurrentPage':'" + CurrentPage + "', 'IsBookMark':'" + IsBookMark + "'}"),

In this case i get complete Json format value in "CurrentPage" parameter inside action method

Below is the screen shot for the same.

Please suggest.

解决方案

in Asp.Net core the passed parameter value is null in controller

From this doc, you could find:

Don't apply [FromBody] to more than one parameter per action method. Once the request stream is read by an input formatter, it's no longer available to be read again for binding other [FromBody] parameters.

To achieve your requirement of passing multiple data from JS client side to controller action method, you can try following code snippet.

[HttpPost]
public IActionResult HighlightCurrentMenu([FromBody]PageInfo pageInfo)
{
    var currentPage = pageInfo?.CurrentPage;
    var isBookMark = pageInfo?.IsBookMark;

    //...
    //code logic here

    return Json(true);
}

PageInfo class

public class PageInfo
{
    public string CurrentPage { get; set; }
    public bool IsBookMark { get; set; }
}

On JS client side

data: JSON.stringify({ "CurrentPage": CurrentPage, "IsBookMark": IsBookMark }),

Test Result

这篇关于jQuery Ajax调用传递的参数在Asp.net MVC Core中始终为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆