Skip to main content
 首页 » 编程设计

c#之mvc Controller 参数在ajax调用中获取空值

2024年02月25日41luoye11

我正在像这样进行ajax调用

 deleteAge(item: any): string { 
 
           $.ajax({ 
                method: "POST", 
                url: 'DeleteAge', 
                data: item.Id(), 
                success: (response) => { 
                    toastr.success("ready soon"); 
                }, 
                error: (event) => { 
                    toastr.error("Error occured "); 
                } 
            }).always(() => { 
 
                }); 
 
 
        return ""; 
 
 
    } 

这是我在 Controller 中的方法,目前几乎没有实现

 [HttpPost] 
    public ActionResult DeleteAge(string id) 
    { 
        throw new Exception(); 
    } 

当我运行代码然后在我的 Controller 中我没有得到任何 id.. 它是空的。当我调试我的 javascript 代码时,此代码 item.Id() 不是空白。即使我将硬编码值传递给 ajax 调用中的数据, Controller 仍然为空。怎么了?

请您参考如下方法:

而不是使用:

data: item.Id(), 

我建议使用:

data: { id: item.Id()}, 

通过这种方式,id 值与 id 名称相关联 - 允许模型绑定(bind)正常工作。