在MVC3控制器方法阿贾克斯后的数据为空控制器、为空、方法、数据

2023-09-06 16:34:39 作者:无爱㈠身轻

我的一个jQuery的AJAX的帖子发出后的数据为我的。NET MVC3控制器的方法,但在控制器的方法,数据显示为空。我使用几乎相同的方法,身体其他许多AJAX的职位,他们都做工精细,所以我不知道发生了什么。

阿贾克斯后:

  $。阿贾克斯({
    网址:'/实体/相关',
    键入:POST,
    数据类型:JSON,
    的contentType:应用程序了/ JSON;字符集= UTF-8,
    数据:{primaryEntityId形式:parseInt(entityParentId,10),relatedEntityId:_createdId},
    成功:函数(数据)
    {
        //做的东西
    },
    错误:函数()
    {
        //抛出错误
    },
    完成:函数()
    {
        //做更多的东西
    }
});
 

控制器的方法:

  [HttpPost]
公众诠释相关(INT primaryEntityId,INT relatedEntityId)
{
    返回relationshi prepository.Add(primaryEntityId,relatedEntityId);
}
 

现在的问题是,当我打破了相关方法,primaryEntityId和relatedEntityId都为空,尽管在Firebug后的数据,它表明{primaryEntityId:13,relatedEntityId:486},已张贴的方法

任何建议或想法,为什么后看起来不错,但控制器不拾取数据?

解决方案   

,但在控制器的方法,该数据显示为空

立得空间为某工厂打造室内外无人搬运智能解决方案

这是不可能的,因为的Int32 是值类型和值类型在.NET中不能。你可能意味着它们被分配到缺省值。无论如何,

这个问题是有关您在AJAX请求设置了的contentType 参数。您需要删除它,因为你没有发送JSON,但标准应用程序/ x-WWW的形式urlen codeD 要求:

  $。阿贾克斯({
    网址:'/实体/相关',
    键入:POST,
    数据类型:JSON,
    数据: {
        primaryEntityId形式:parseInt(entityParentId,10),
        relatedEntityId:_createdId
    },
    成功:函数(数据)
    {
        //做的东西
    },
    错误:函数()
    {
        //抛出错误
    },
    完成:函数()
    {
        //做更多的东西
    }
});
 

如果您要发送一个JSON请求定义视图模式:

 公共类RelateViewModel
{
    公众诠释PrimaryEntityId {获得;组; }
    公众诠释RelatedEntityId {获得;组; }
}
 

然后让你的控制器利用这个视图模型作为参数:

  [HttpPost]
公众诠释相关(RelateViewModel模型)
{
    返回relationshi prepository.Add(model.PrimaryEntityId,model.RelatedEntityId);
}
 

和最后发送一个真正的JSON请求(使用 JSON.stringify 法):

  $。阿贾克斯({
    网址:'/实体/相关',
    键入:POST,
    数据类型:JSON,
    的contentType:应用/ JSON的;字符集= UTF-8,
    数据:JSON.stringify({
        primaryEntityId形式:parseInt(entityParentId,10),
        relatedEntityId:_createdId
    }),
    成功:函数(数据)
    {
        //做的东西
    },
    错误:函数()
    {
        //抛出错误
    },
    完成:函数()
    {
        //做更多的东西
    }
});
 

One of my jquery ajax posts sends post data to my .NET MVC3 controller method, but at the controller method, the data shows up as null. I have many other ajax posts that use practically the same method body, and they all work fine, so I'm not sure what is happening.

Ajax post:

$.ajax({
    url: '/Entity/Relate',
    type: 'POST',
    dataType: 'json',
    contentType: 'applicaiton/json; charset=utf-8',
    data: { primaryEntityId: parseInt(entityParentId, 10), relatedEntityId: _createdId },
    success: function (data)
    {
        //do stuff
    },
    error: function ()
    {
        // throw error
    },
    complete: function ()
    {
        //do more stuff
    }
});

Controller method:

[HttpPost]
public int Relate(int primaryEntityId, int relatedEntityId)
{
    return relationshipRepository.Add(primaryEntityId, relatedEntityId);
}

The problem is when I break on the Relate method, primaryEntityId and relatedEntityId are null, even though in the post data in Firebug, it shows that {primaryEntityId: 13, relatedEntityId: 486} have been posted to the method.

Any suggestions or ideas as to why the post looks good, but the controller isn't picking up the data?

解决方案

but at the controller method, the data shows up as null

That's not possible because Int32 is a value type and value types in .NET cannot be null. You probably meant that they are assigned to the default value. Anyway.

The problem is related to the contentType parameter that you have set in your AJAX request. You need to remove it because you are not sending JSON but a standard application/x-www-form-urlencoded request:

$.ajax({
    url: '/Entity/Relate',
    type: 'POST',
    dataType: 'json',
    data: { 
        primaryEntityId: parseInt(entityParentId, 10), 
        relatedEntityId: _createdId 
    },
    success: function (data)
    {
        //do stuff
    },
    error: function ()
    {
        // throw error
    },
    complete: function ()
    {
        //do more stuff
    }
});

If you want to send a JSON request define a view model:

public class RelateViewModel
{
    public int PrimaryEntityId { get; set; }
    public int RelatedEntityId { get; set; }
}

then have your controller take this view model as argument:

[HttpPost]
public int Relate(RelateViewModel model)
{
    return relationshipRepository.Add(model.PrimaryEntityId, model.RelatedEntityId);
}

and finally send a real JSON request (using the JSON.stringify method):

$.ajax({
    url: '/Entity/Relate',
    type: 'POST',
    dataType: 'json',
    contentType: 'application/json;charset=utf-8',
    data: JSON.stringify({ 
        primaryEntityId: parseInt(entityParentId, 10), 
        relatedEntityId: _createdId 
    }),
    success: function (data)
    {
        //do stuff
    },
    error: function ()
    {
        // throw error
    },
    complete: function ()
    {
        //do more stuff
    }
});

 
精彩推荐
图片推荐